How to combine multiple change events VBA

Empirer

New Member
Joined
Oct 15, 2020
Messages
11
Office Version
  1. 2016
Platform
  1. Windows
Hi all,

Currently I try to set multiple change events criteria on one worksheet. However, only the first criteria with range (I:I) seems to be working.

Private Sub Worksheet_Change(ByVal Target As Range)
Dim A As Range: Set A = Range("I:I")
Dim v As String
If Intersect(Target, A) Is Nothing Then
Exit Sub

Application.EnableEvents = False
v = Target.Value
If v <> "" Then Target.Offset(0, -8) = Now()
If v = "" Then Target.Offset(0, -8) = ""
Application.EnableEvents = True
End If

Dim B As Range: Set B = Range("M:M")
Dim w As String
If Intersect(Target, B) Is Nothing Then
Exit Sub
Application.EnableEvents = False
w = Target.Value
If w = "Completed" Then Target.Offset(0, -1) = Now()
If w = "" Then Target.Offset(0, -1) = ""
Application.EnableEvents = True
End If
 

Excel Facts

Which lookup functions find a value equal or greater than the lookup value?
MATCH uses -1 to find larger value (lookup table must be sorted ZA). XLOOKUP uses 1 to find values greater and does not need to be sorted.
Hi & welcome to MrExcel.
How about
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
   If Not Intersect(Target, Range("I:I")) Is Nothing Then
      If Target <> "" Then
         Target.Offset(0, -8) = Now
      Else
         Target.Offset(0, -8) = ""
      End If
   ElseIf Not Intersect(Target, Range("M:M")) Is Nothing Then
      If Target = "Completed" Then
         Target.Offset(0, -1) = Now
      ElseIf Target = "" Then
         Target.Offset(0, -1) = ""
      End If
   End If
End Sub
 
Upvote 0
Thanks, the code works, but just to confirm I want the date stamps to not change over time.
Don't I have to add the Application.EnableEvents part?
 
Upvote 0
With that code there is no need to disable events, as the code is changing cells outside of columns I & M.
The date/time stamps will not change unless you change a value in either column I or M.
 
Upvote 0

Forum statistics

Threads
1,214,788
Messages
6,121,603
Members
449,038
Latest member
Arbind kumar

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