Ambiguous name detected Worksheet_Change

2019-09-17 15:37发布

I'm having a problem with a macro because it give me Ambiguous name detected Worksheet_Change . If the user enter a value on any cell under column B it will run automatically a macro and if the user enter a value on column F it will run automatically another macro but I do not know how to fix this error . Please the the code below

Private Sub Worksheet_Change(ByVal Target As Range)
    If Not Intersect(Target, Columns("B")) Is Nothing Then
        On Error GoTo Safe_Exit
        Application.EnableEvents = False
        Dim rng As Range
        For Each rng In Intersect(Target, Columns("B"))
            If rng.Row > 2 Then ' your sample code seemed to suggest that this should start on row 3 and higher
                Call MyMacro(rng.Row)
            End If
        Next rng
    End If
Safe_Exit:
    Application.EnableEvents = True
End Sub

Sub MyMacro(rw As Long)
    If Range("B" & rw) = "" Then
        MsgBox "Ingrese El account Number"
    Else
        Range("J" & rw & ":K" & rw) = Range("J" & rw & ":K" & rw).Value
    End If
End Sub

Private Sub Worksheet_Change(ByVal Target As Range)
    If Not Intersect(Target, Columns("F")) Is Nothing Then
        On Error GoTo Safe_Exit
        Application.EnableEvents = False
        Dim rng As Range
        For Each rng In Intersect(Target, Columns("F"))
            If rng.Row > 3 Then ' your sample code seemed to suggest that this should start on row 3 and higher
                Call Foolish(rng.Row)
            End If
        Next rng
    End If
Safe_Exit:
    Application.EnableEvents = True
End Sub

Sub Foolish(rw As Long)
    If Range("F" & rw) = "" Then
        MsgBox "Ingrese El account Number"
    Else
        Range("G" & rw & ":H" & rw) = Range("G" & rw & ":H" & rw).Value
    End If
End Sub

1条回答
姐就是有狂的资本
2楼-- · 2019-09-17 16:23

You have two Worksheet_change() subs happening in your sheet. Copy the contents of one of those subroutines and paste it inside the other one so there is only one worksheet_change event.

For example:

Private Sub Worksheet_Change(ByVal Target As Range)
    If Not Intersect(Target, Columns("B")) Is Nothing Then
        On Error GoTo Safe_Exit
        Application.EnableEvents = False
        Dim rng As Range
        For Each rng In Intersect(Target, Columns("B"))
            If rng.Row > 2 Then ' your sample code seemed to suggest that this should start on row 3 and higher
                Call MyMacro(rng.Row)
            End If
        Next rng
    End If

    If Not Intersect(Target, Columns("F")) Is Nothing Then
        On Error GoTo Safe_Exit
        Application.EnableEvents = False
        Dim rng As Range
        For Each rng In Intersect(Target, Columns("F"))
            If rng.Row > 3 Then ' your sample code seemed to suggest that this should start on row 3 and higher
                Call Foolish(rng.Row)
            End If
        Next rng
    End If

Safe_Exit:
    Application.EnableEvents = True
End Sub

Sub MyMacro(rw As Long)
    If Range("B" & rw) = "" Then
        MsgBox "Ingrese El account Number"
    Else
        Range("J" & rw & ":K" & rw) = Range("J" & rw & ":K" & rw).Value
    End If
End Sub
查看更多
登录 后发表回答