VBA Compile Error: Ambiguous name detected

Neurotic

New Member
Joined
Apr 3, 2013
Messages
2
Hello

Im new to vba and i am getting Compile Error: Ambiguous name detected

any help would be Appreciated

Cheers
Code:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim rngDest As Range
Set rngDest = Worksheets("Archive").Range("rngDest")
If Not Intersect(Target, Worksheets("Active").Range("rngTrigger")) Is Nothing Then
     If IsDate(Target) Then
        Application.EnableEvents = False
        Target.EntireRow.Select
        Selection.Cut
        rngDest.Insert Shift:=xlDown
        Selection.Delete
        Application.EnableEvents = True
    End If
End If
End Sub
 
Private Sub Worksheet_Change(ByVal Target As Range)
Dim rngDest2 As Range
Set rngDest2 = Worksheets("HoldingBay").Range("rngDest2")
If Not Intersect(Target, Worksheets("Active").Range("rngTrigger2")) Is Nothing Then
     If IsDate(Target) Then
        Application.EnableEvents = False
        Target.EntireRow.Select
        Selection.Cut
        rngDest2.Insert Shift:=xlDown
        Selection.Delete
        Application.EnableEvents = True
    End If
End If
End Sub
 

Excel Facts

Who is Mr Spreadsheet?
Author John Walkenbach was Mr Spreadsheet until his retirement in June 2019.
Two "Private Sub Worksheet_Change(ByVal Target As Range)" events within the same worksheet

You can only have one and with that one handle everything you need when the worksheet changes.

Like so...
Code:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim rngDest As Range
Set rngDest = Worksheets("Archive").Range("rngDest")
If Not Intersect(Target, Worksheets("Active").Range("rngTrigger")) Is Nothing Then
     If IsDate(Target) Then
        Application.EnableEvents = False
        Target.EntireRow.Select
        Selection.Cut
        rngDest.Insert Shift:=xlDown
        Selection.Delete
        Application.EnableEvents = True
    End If
End If

Dim rngDest2 As Range
Set rngDest2 = Worksheets("HoldingBay").Range("rngDest2")
If Not Intersect(Target, Worksheets("Active").Range("rngTrigger2")) Is Nothing Then
     If IsDate(Target) Then
        Application.EnableEvents = False
        Target.EntireRow.Select
        Selection.Cut
        rngDest2.Insert Shift:=xlDown
        Selection.Delete
        Application.EnableEvents = True
    End If
End If
End Sub
 
Last edited:
Upvote 0
Two "Private Sub Worksheet_Change(ByVal Target As Range)" events within the same worksheet

You can only have one and with that one handle everything you need when the worksheet changes.

Like so...
Code:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim rngDest As Range
Set rngDest = Worksheets("Archive").Range("rngDest")
If Not Intersect(Target, Worksheets("Active").Range("rngTrigger")) Is Nothing Then
     If IsDate(Target) Then
        Application.EnableEvents = False
        Target.EntireRow.Select
        Selection.Cut
        rngDest.Insert Shift:=xlDown
        Selection.Delete
        Application.EnableEvents = True
    End If
End If

Dim rngDest2 As Range
Set rngDest2 = Worksheets("HoldingBay").Range("rngDest2")
If Not Intersect(Target, Worksheets("Active").Range("rngTrigger2")) Is Nothing Then
     If IsDate(Target) Then
        Application.EnableEvents = False
        Target.EntireRow.Select
        Selection.Cut
        rngDest2.Insert Shift:=xlDown
        Selection.Delete
        Application.EnableEvents = True
    End If
End If
End Sub


Perfect, So simple!

thanks for the fast reply

Cheers
 
Upvote 0
Hi! Excel isn't my usual environment (I'm architect and usually work in simple stuff in Excel) but now I'm trying to create a spreadsheet to feed information to a script, some of the parameter are colours, the input are numbers for RGB in separate coloumns, so I want to have a control column where the filling colour of the cells is determined by the values in the corresponding columns.

I found a piece of code here in a post that helped me a lot, but it is limited to one column and one set of values, I would like to define many columns and many sets of values, but for now I haven't managed to tweak the code to do this. I'll post here the original code and next my version of the tweaked code that shows the error mentioned above.

Here the original:

Private Sub Worksheet_Change(ByVal Target As Range) Dim rng As Range, cell As Range Dim i As Long Set rng = Intersect(Target, Range("A:C")) If Not rng Is Nothing Then On Error Resume Next For Each cell In Target.Columns(1).Cells If Application.CountA(Range("A" & cell.Row & ":C" & cell.Row)) < 3 Or _ Application.Count(Range("A" & cell.Row & ":C" & cell.Row)) < 3 Then GoTo next_row Cells(cell.Row, "D").Interior.Color = _ RGB(Cells(cell.Row, "A").Value, Cells(cell.Row, "B").Value, Cells(cell.Row, "C").Value) next_row: Next cell End If End Sub
 
Upvote 0
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range) Dim rng As Range, cell As Range Dim i As Long Set rng = Intersect(Target, Range("A:C")) If Not rng Is Nothing Then    On Error Resume Next    For Each cell In Target.Columns(1).Cells        If Application.CountA(Range("A" & cell.Row & ":C" & cell.Row)) < 3 Or _            Application.Count(Range("A" & cell.Row & ":C" & cell.Row)) < 3 Then GoTo next_row        Cells(cell.Row, "D").Interior.Color = _            RGB(Cells(cell.Row, "A").Value, Cells(cell.Row, "B").Value, Cells(cell.Row, "C").Value) next_row:    Next cell End If End Sub
 
Upvote 0
And here is "my version" of this:


VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim rng As Range, cell As Range
Dim i As Long

Set rng = Intersect(Target, Range("K:M"))
If Not rng Is Nothing Then
    On Error Resume Next
    For Each cell In Target.Columns(1).Cells
        If Application.CountA(Range("K" & cell.Row & ":M" & cell.Row)) < 3 Or _
            Application.Count(Range("K" & cell.Row & ":M" & cell.Row)) < 3 Then GoTo next_row
        Cells(cell.Row, "J").Interior.Color = _
            RGB(Cells(cell.Row, "K").Value, Cells(cell.Row, "L").Value, Cells(cell.Row, "M").Value)
next_row:
    Next cell
End If


Dim rng2 As Range, cell As Range
Dim i As Long

Set rng2 = Intersect(Target, Range("P:R"))
If Not rng Is Nothing Then
    On Error Resume Next
    For Each cell In Target.Columns(1).Cells
        If Application.CountA(Range("P" & cell.Row & ":R" & cell.Row)) < 3 Or _
            Application.Count(Range("P" & cell.Row & ":R" & cell.Row)) < 3 Then GoTo next_row
        Cells(cell.Row, "O").Interior.Color = _
            RGB(Cells(cell.Row, "P").Value, Cells(cell.Row, "Q").Value, Cells(cell.Row, "R").Value)
next_row:
    Next cell
End If

End Sub
 
Upvote 0
Well, sorry for the spam, but finally it was possible to fix the code. A colleague of mine did it. Here is the final result.


VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim rng As Range, cell As Range


' Defining Shading Color

Set rng = Intersect(Target, Range("K:M"))
If Not rng Is Nothing Then
    On Error Resume Next
    For Each cell In Target.Columns(1).Cells
        If Application.CountA(Range("K" & cell.Row & ":M" & cell.Row)) < 3 Or _
            Application.count(Range("K" & cell.Row & ":M" & cell.Row)) < 3 Then GoTo next_row_1
        Cells(cell.Row, "J").Interior.Color = _
            RGB(Cells(cell.Row, "K").Value, Cells(cell.Row, "L").Value, Cells(cell.Row, "M").Value)
next_row_1:
    Next cell
End If


' Defining Surface Foreground Pattern Color

Set rng = Intersect(Target, Range("P:R"))
If Not rng Is Nothing Then
    On Error Resume Next
    For Each cell In Target.Columns(1).Cells
        If Application.CountA(Range("P" & cell.Row & ":R" & cell.Row)) < 3 Or _
            Application.count(Range("P" & cell.Row & ":R" & cell.Row)) < 3 Then GoTo next_row_2
        Cells(cell.Row, "O").Interior.Color = _
            RGB(Cells(cell.Row, "P").Value, Cells(cell.Row, "Q").Value, Cells(cell.Row, "R").Value)
next_row_2:
    Next cell
End If


' Defining Cut Foreground Pattern Color

Set rng = Intersect(Target, Range("U:W"))
If Not rng Is Nothing Then
    On Error Resume Next
    For Each cell In Target.Columns(1).Cells
        If Application.CountA(Range("U" & cell.Row & ":W" & cell.Row)) < 3 Or _
            Application.count(Range("U" & cell.Row & ":W" & cell.Row)) < 3 Then GoTo next_row_2
        Cells(cell.Row, "T").Interior.Color = _
            RGB(Cells(cell.Row, "U").Value, Cells(cell.Row, "V").Value, Cells(cell.Row, "W").Value)
next_row_3:
    Next cell
End If

End Sub
 
Upvote 0

Forum statistics

Threads
1,223,315
Messages
6,171,412
Members
452,399
Latest member
oranges

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top