Compile Error

csenor

Board Regular
Joined
Apr 10, 2013
Messages
168
Office Version
  1. 365
Platform
  1. Windows
I'm adding two VBA codes to a tab in a worksheet to either add a creation date or when something in the row is updated, add a modified date. I found the VBA code in another post and copied/pasted it into the VBA sheet. When I attempt to run it, I get a Compile Error: Ambiguous name detected. Can both sets of code exist in the same worksheet? I tried renaming the sub, but it didn't fix anything. I tried combining both codes, but it didn't produce a date in either column. Any help would be appreciated.


Code that works:

VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)

    Dim cell As Range
   
    Application.ScreenUpdating = False
   
    For Each cell In Target
        Application.EnableEvents = False
        If cell.Row > 1 And cell.Column > 8 Then Cells(cell.Row, "AF") = Now()
        Application.EnableEvents = True
    Next cell
   
        Application.ScreenUpdating = True
   
End Sub



Code that doesn't work:
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)

    Dim cell As Range
   
    Application.ScreenUpdating = False
   
    For Each cell In Target
        Application.EnableEvents = False
        If cell.Row > 1 And cell.Column > 8 Then Cells(cell.Row, "AF") = Now()
        Application.EnableEvents = True
    Next cell
   
   
For Each cell In Target
        Application.EnableEvents = False
        If cell.Row = 1 And cell.Column > 8 Then Cells(cell.Row, "AE") = Now()
        Application.EnableEvents = True
    Next cell

    Application.ScreenUpdating = True
   
End Sub
 

Excel Facts

Excel Joke
Why can't spreadsheets drive cars? They crash too often!
You cannot have two procedures (Subs) in the same module with the same name.
You also cannot change the name of Event Procedure code. It MUST be saved exactly as it first appears, or else it will not be run automatically.

You can out two blocks of code in the same procedure.
Or, you can create multiple procedures that are NOT event procedure codes, and call those procedures from your event procedure code, i.e.
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    Call Macro1
    Call Macro2
End Sub
 
Upvote 0
You can have only one routine named
Worksheet_Change(ByVal Target As Range)

And if it is named anything else, it won't automatically run when the Change event fires.

Remove both of those routines and try this

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

    Application.ScreenUpdating = False
    Application.EnableEvents = False
    For Each cell In Target
       If cell.Row > 1 And cell.Column > 8 Then 
            Cells(cell.Row, "AF") = Now()
       ElseIf cell.Row = 1 And cell.Column > 8 Then 
            Cells(cell.Row, "AE") = Now()
        End IF
    Next cell
    Application.EnableEvents = True
    Application.ScreenUpdating = True
   
End Sub
 
Upvote 0
1.
You can have only one routine named
Worksheet_Change(ByVal Target As Range)

And if it is named anything else, it won't automatically run when the Change event fires.

Remove both of those routines and try this

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

    Application.ScreenUpdating = False
    Application.EnableEvents = False
    For Each cell In Target
       If cell.Row > 1 And cell.Column > 8 Then
            Cells(cell.Row, "AF") = Now()
       ElseIf cell.Row = 1 And cell.Column > 8 Then
            Cells(cell.Row, "AE") = Now()
        End IF
    Next cell
    Application.EnableEvents = True
    Application.ScreenUpdating = True
 
End Sub


Am I putting this in the sheet itself or in a module for the workbook? I have several tabs in the workbook that I don't want to be affected.
 
Upvote 0
That code would go in the code module for the sheet where you want it to work.

The "code that works", wherever that code was would be the place to put the code I posted (after you delete the OP routine).
 
Upvote 0

Forum statistics

Threads
1,214,522
Messages
6,120,022
Members
448,939
Latest member
Leon Leenders

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