Multiple worksheet_change Events in One Worksheet

mjg777

New Member
Joined
Mar 19, 2018
Messages
5
Hello,
I would like to have the below code run twice within the same worksheet. As seen now, it's set to place a timestamp in a cell four columns to the left of column "S". I would like to run this a second time and it would need to be 7 columns to the left of column "U". Thanks in advance!

Private Sub Worksheet_Change(ByVal Target As Range)
Dim WorkRng As Range
Dim Rng As Range
Dim xOffsetColumn As Integer
Set WorkRng = Intersect(Application.ActiveSheet.Range("S:S"), Target)
xOffsetColumn = -4
If Not WorkRng Is Nothing Then
Application.EnableEvents = False
For Each Rng In WorkRng
If Not VBA.IsEmpty(Rng.Value) Then
Rng.Offset(0, xOffsetColumn).Value = Now
Rng.Offset(0, xOffsetColumn).NumberFormat = "mm-dd-yyyy, hh:mm:ss"
Else
Rng.Offset(0, xOffsetColumn).ClearContents
End If
Next
Application.EnableEvents = True
End If
End Sub
 

Excel Facts

Create a chart in one keystroke
Select the data and press Alt+F1 to insert a default chart. You can change the default chart to any chart type
Try this:-
Code:
Private Sub Worksheet_Change(ByVal Target As Range)
 Dim WorkRng As Range
 Dim Rng As Range
 Dim xOffsetColumn As Integer
 Static c
 c = c + 1
 xOffsetColumn = IIf(c Mod 2 = 0, -4, -7)
 Set WorkRng = Intersect(Application.ActiveSheet.Range("S:S"), Target)
 
 If Not WorkRng Is Nothing Then
 Application.EnableEvents = False
 For Each Rng In WorkRng
 If Not VBA.IsEmpty(Rng.Value) Then
 Rng.Offset(0, xOffsetColumn).Value = Now
 Rng.Offset(0, xOffsetColumn).NumberFormat = "mm-dd-yyyy, hh:mm:ss"
 Else
 Rng.Offset(0, xOffsetColumn).ClearContents
 End If
 Next
  c = IIf(c = 2, 0, c)
 Application.EnableEvents = True
 End If
 End Sub
 
Upvote 0
Hello – Thank you for your quick response! After replacing the code with your suggestion, column “O” is updating with a timestamp once column “S” is populated with a value. However, column “N” is not populating when column “U” is populated with a value. Both columns “S” & “U” will be populated on each row in my worksheet so I need columns “N” and “O” to populate with corresponding timestamps.
 
Upvote 0
Try this:-
Code:
Private Sub Worksheet_Change(ByVal Target As Range)
 Dim WorkRng As Range
 Dim Rng As Range
 Dim xOffsetColumn As Integer
 If Not Intersect(Target, Union(Range("S:S"), Range("U:U"))) Is Nothing Then
    Application.EnableEvents = False
    xOffsetColumn = IIf(Target.Column = 19, -4, -7)
        If Not VBA.IsEmpty(Target.Value) Then
            Target.Offset(0, xOffsetColumn).Value = Now
            Target.Offset(0, xOffsetColumn).NumberFormat = "mm-dd-yyyy, hh:mm:ss"
        Else
            Target.Offset(0, xOffsetColumn).ClearContents
        End If
    Application.EnableEvents = True
 End If
 End Sub
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,180
Members
453,021
Latest member
Justyna P

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