+ Reply to Thread
Results 1 to 2 of 2

Error code when I run 2 sets of code.

  1. #1
    Forum Contributor
    Join Date
    11-26-2008
    Location
    Ohio
    MS-Off Ver
    2016
    Posts
    326

    Error code when I run 2 sets of code.

    I have two sets of codes and there must be a name problem.
    Each works OK on their own but when I put them on the same sheet I get a compile error: "Ambigous Name Detected: Worksheet_SelectionChange" when it tries to run.

    Can both run on the same sheet or do I just have to change a name or something in one of the codes?

    Thanks / Rich
    ------------------------------------------------------------

    Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    Const WS_RANGE As String = "A1:A300" '<=== change Range of cells to suit

    On Error GoTo err_handler
    Application.EnableEvents = False
    If Not Application.Intersect(Target, Range(WS_RANGE)) Is Nothing Then
    With Target
    .Font.Name = "Good"
    Select Case .Value
    Case "Good": .Value = "Fair"
    Case "Fair": .Value = "Bad"
    Case "Bad": .Value = ""
    Case Else: .Value = "Good"

    End Select
    .Offset(2, 0).Select
    End With
    End If
    err_handler:
    Application.EnableEvents = True
    End Sub
    --------------------------------------------------------------
    Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    Const WS_RANGE As String = "B1:B300" '<=== change Range of cells to suit

    On Error GoTo err_handler
    Application.EnableEvents = False
    If Not Application.Intersect(Target, Range(WS_RANGE)) Is Nothing Then
    With Target
    .Font.Name = "X"
    Select Case .Value
    Case "X": .Value = ""
    Case "": .Value = "X"
    Case Else: .Value = ""

    End Select
    .Offset(1, 0).Select
    End With
    End If
    err_handler:
    Application.EnableEvents = True
    End Sub[/SIZE]

  2. #2
    Valued Forum Contributor
    Join Date
    11-11-2008
    Location
    Euro
    MS-Off Ver
    2007, 2010
    Posts
    470
    Quote Originally Posted by richard11153 View Post
    I have two sets of codes and there must be a name problem.
    Each works OK on their own but when I put them on the same sheet I get a compile error: "Ambigous Name Detected: Worksheet_SelectionChange" when it tries to run.

    Can both run on the same sheet or do I just have to change a name or something in one of the codes?

    Thanks / Rich
    ------------------------------------------------------------
    You should use one IF to make a direction to your options, that becomes as following code

    Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    Const WS_RANGE1 As String = "A1:A300" '<=== change Range of cells to suit
    Const WS_RANGE2 As String = "B1:B300" '<=== change Range of cells to suit


    On Error GoTo err_handler
    Application.EnableEvents = False


    If Not Application.Intersect(Target, Range(WS_RANGE1)) Is Nothing Then

    With Target
    .Font.Name = "Good"
    Select Case .Value
    Case "Good": .Value = "Fair"
    Case "Fair": .Value = "Bad"
    Case "Bad": .Value = ""
    Case Else: .Value = "Good"

    End Select
    .Offset(2, 0).Select
    End With
    ElseIf Application.Intersect(Target, Range(WS_RANGE2)) Is Nothing Then
    With Target
    .Font.Name = "X"
    Select Case .Value
    Case "X": .Value = ""
    Case "": .Value = "X"
    Case Else: .Value = ""

    End Select
    .Offset(1, 0).Select
    End With
    End If
    err_handler:
    Application.EnableEvents = True
    End Sub

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1