How to combine multiple change events VBA

Empirer

New Member
Joined
Oct 15, 2020
Messages
5
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
 

Some videos you may like

Excel Facts

How to show all formulas in Excel?
Press Ctrl+` to show all formulas. Press it again to toggle back to numbers. The grave accent is often under the tilde on US keyboards.

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
55,412
Office Version
  1. 365
Platform
  1. Windows
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
 

Empirer

New Member
Joined
Oct 15, 2020
Messages
5
Office Version
  1. 2016
Platform
  1. Windows
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?
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
55,412
Office Version
  1. 365
Platform
  1. Windows
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.
 

Watch MrExcel Video

Forum statistics

Threads
1,127,328
Messages
5,624,036
Members
416,007
Latest member
csf

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
Top