Excel VBA Course
Excel VBA Course - From Beginner to Expert

200+ Video Lessons
50+ Hours of Video
200+ Excel Guides

Become a master of VBA and Macros in Excel and learn how to automate all of your tasks in Excel with this online course. (No VBA experience required.)

View Course

Worksheet_Change

0

Can you have more that one "Worksheet_Change" in a Sheet?  When I try I get "Ambigous name detedted"Worksheet_Change"

I have :

Private Sub Worksheet_Change(ByVal Target As Range)

If Target.Address = Range("p6").Address Then

NewLine

  End If

  End Sub

I want to add to the same sheet:

 Sub Worksheet_Change(ByVal Target As Range)

'Timestamp Data

'   TeachExcel.com

Dim myTableRange As Range

Dim myDateTimeRange As Range

Dim myUpdatedRange As Range

'Your data table range

Set myTableRange = Range("C26")

'Check if the changed cell is in the data tabe or not.

If Intersect(Target, myTableRange) Is Nothing Then Exit Sub

'Stop events from running

Application.EnableEvents = False

'Column for the date/time

Set myDateTimeRange = Range("UPDATE")

'Column for last updated date/time

'Set myUpdatedRange = Range("F" & Target.Row)

Set myUpdatedRange = Range("UPDATE")

'Determine if the input date/time should change

If myDateTimeRange.Value = "" Then

    myDateTimeRange.Value = Now

End If

'Update the updated date/time value

myUpdatedRange.Value = Now

If IsEmpty(Target.Value) Then

myDateTimeRange.Value = ""

'myUpdatedRange.Value = ""

End If

'Turn events back on

Application.EnableEvents = True

End Sub

Answer
Discuss

Discussion

Please put CODE tags around your code. To do that, edit your post, select your code and click the CODE button.
don (rep: 1989) Mar 21, '22 at 7:33 pm
Add to Discussion

Answers

0
Selected Answer

Orkrj

You can combine the two codes within a IF statement within (a single) Worksheet_Change macro, like this:

Private Sub Worksheet_Change(ByVal Target As Range)

If Target.Address = Range("p6").Address Then
    ' do something if P6 was changed
    NewLine

    Else
    ' otherwise do this...

    'Timestamp Data

    '   TeachExcel.com

    Dim myTableRange As Range
    '.... (<< rest of Timestamp code>>

    'Turn events back on

    Application.EnableEvents = True


End If


End Sub

... where I've added comments in bold and left you to identify/paste the missing lines of code.

Hope this makes sense and helps.

Discuss

Discussion

This worked just fine

Thanks
orkrj (rep: 14) Mar 21, '22 at 9:30 pm
Add to Discussion


Answer the Question

You must create an account to use the forum. Create an Account or Login