Two Change Events on Same Sheet

mtaylor50

New Member
Joined
Oct 4, 2004
Messages
28
In attempting to place two change events in the same worksheet, I get an error message saying "Ambiguous Name". As a VBA newbie, I've been unable to correctly interpret the Help section (despite many trials and errors). Here's what's supposed to happen:
1) When a customer orders a product, staff enters the $ amount in column K. This activates a date-stamp in column A.
2) When the customer's payment is received, staff enters the $ amount in column L, which activates a date-stamp in column X.

Each of the codes below works fine when it is the only change event. But when I add the second event, the "Ambiguous Name" message appears. I've attempted numerous edits without success. Any help is much appreciated. Thank you.
Mark

Private Sub Worksheet_Change(ByVal Target As Range)
Dim rng As Range
' Only look at single cell changes
If Target.Count > 1 Then Exit Sub
Set rng = Range("K:K")
If Intersect(Target, rng) Is Nothing Then Exit Sub
ActiveSheet.Unprotect ("e")
Target.Offset(0, -10) = Date
ActiveSheet.Protect ("e")
End Sub
------------------------------------------------------------------------
Private Sub Worksheet_Change(ByVal Target As Range)
Dim rng As Range
' Only look at single cell changes
If Target.Count > 1 Then Exit Sub
Set rng = Range("L:L")
If Intersect(Target, rng) Is Nothing Then Exit Sub
ActiveSheet.Unprotect ("e")
Target.Offset(0, 12) = Date
ActiveSheet.Protect ("e")
End Sub
 

Excel Facts

What did Pito Salas invent?
Pito Salas, working for Lotus, popularized what would become to be pivot tables. It was released as Lotus Improv in 1989.
You can only have one change event in existance, you had two, so vb didn't know which one to look at. I have adjusted your code so that it will work all together.

Code:
Private Sub Worksheet_Change(ByVal Target As Range) 
Dim rng As Range 
' Only look at single cell changes 
If Target.Count > 1 Then Exit Sub 
Set rng = Range("K:K") 
If Intersect(Target, rng) Is Nothing Then Exit Sub 
ActiveSheet.Unprotect ("e") 
Target.Offset(0, -10) = Date 
ActiveSheet.Protect ("e") 

Set rng = Range("L:L") 
If Intersect(Target, rng) Is Nothing Then Exit Sub 
ActiveSheet.Unprotect ("e") 
Target.Offset(0, 12) = Date 
ActiveSheet.Protect ("e") 
End Sub

HTH
Cal
 
Upvote 0
Thanks, Cal.
Yes, I had tried that earlier. I also pasted your amended code into the VBE with the same result, which was partially successful: I did not get the "Ambiguous Name" message. However, while the date-stamp activated in column A after entering $ in column K, the date-stamp did not activate in column X after entering $ in column L. Any other suggestion? Thanks.
 
Upvote 0
Try this.
Code:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim rng1 As Range
Dim rng2 As Range
    ' Only look at single cell changes
    If Target.Count > 1 Then Exit Sub
    
    Set rng1 = Range("K:K")
    Set rng2 = Range("L:L")
    
    If Intersect(Target, Union(rng1, rng2)) Is Nothing Then Exit Sub
    
    Application.EnableEvents = False
    Me.Unprotect ("e")
    
    Select Case Target.Column
        Case 11
            Target.Offset(0, -10) = Date
        Case 12
            Target.Offset(0, 12) = Date
    End Select
    
    Application.EnableEvents = True
    Me.Protect ("e")

End Sub
 
Upvote 0

Forum statistics

Threads
1,214,516
Messages
6,119,980
Members
448,934
Latest member
audette89

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