Combining Worksheet Changes

laurens102

New Member
Joined
Dec 7, 2023
Messages
6
Office Version
  1. 365
Platform
  1. Windows
Hello I am trying to combine two worksheet changes that will update separate timestamps based on if their corresponding columns have been changed. The problem I am having is only one timestamp is changed while the other on stays the same.

Private Sub Worksheet_Change1(ByVal Target As Range)
'Update 20140722
Dim WorkRng1 As Range1
Dim Rng1 As Range1
Dim xOffsetColumn As Integer
Set WorkRng1 = Intersect(Application.ActiveSheet.Range1("B:B"), Target)
xOffsetColumn = 3
If Not WorkRng1 Is Nothing Then
Application.EnableEvents = False
For Each Rng1 In WorkRng1
If Not VBA.IsEmpty(Rng1.Value) Then
Rng1.Offset(0, xOffsetColumn).Value = Now
Rng1.Offset(0, xOffsetColumn).NumberFormat = "mm/dd/yyyy, hh:mm:ss"
Else
Rng1.Offset(0, xOffsetColumn).ClearContents
End If
Next
Application.EnableEvents = True

End If

Private Sub Worksheet_Change2(ByVal Target As Range)
'Update 20140722
Dim WorkRng2 As Range2
Dim Rng2 As Range2
Dim xOffsetColumn As Integer
Set WorkRng2 = Intersect(Application.ActiveSheet.Range2("G:G"), Target)
xOffsetColumn = 3
If Not WorkRng2 Is Nothing Then
Application.EnableEvents = False
For Each Rng In WorkRng2
If Not VBA.IsEmpty(Rng2.Value) Then
Rng2.Offset(0, xOffsetColumn).Value = Now
Rng2.Offset(0, xOffsetColumn).NumberFormat = "mm/dd/yyyy, hh:mm:ss"
Else
Rng2.Offset(0, xOffsetColumn).ClearContents
End If
Next
Application.EnableEvents = True

End If
End Sub
 

Excel Facts

Add Bullets to Range
Select range. Press Ctrl+1. On Number tab, choose Custom. Type Alt+7 then space then @ sign (using 7 on numeric keypad)
Welcome to the Board!

Note that in order for the code to run automatically, it MUST be named "Worksheet_Change". You cannot add or remove anything from that name or else it will not run automatically.
And, you are only allowed on "Worksheet_Change" procedure per sheet. So you need to combine your two procedures together.

So you could combine them, something like this:
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)

    Dim WorkRng1 As Range
    Dim Rng1 As Range
    Dim xOffsetColumn As Integer
    Dim WorkRng2 As Range
    Dim Rng2 As Range

    xOffsetColumn = 3

'***FIRST BLOCK***
    Set WorkRng1 = Intersect(Application.ActiveSheet.Range("B:B"), Target)
    If Not WorkRng1 Is Nothing Then
        Application.EnableEvents = False
        For Each Rng1 In WorkRng1
            If Not VBA.IsEmpty(Rng1.Value) Then
                Rng1.Offset(0, xOffsetColumn).Value = Now
                Rng1.Offset(0, xOffsetColumn).NumberFormat = "mm/dd/yyyy, hh:mm:ss"
            Else
                Rng1.Offset(0, xOffsetColumn).ClearContents
            End If
        Next
        Application.EnableEvents = True
    End If

'***SECOND BLOCK***
    Set WorkRng2 = Intersect(Application.ActiveSheet.Range("G:G"), Target)
    If Not WorkRng2 Is Nothing Then
        Application.EnableEvents = False
        For Each Rng2 In WorkRng2
            If Not VBA.IsEmpty(Rng2.Value) Then
                Rng2.Offset(0, xOffsetColumn).Value = Now
                Rng2.Offset(0, xOffsetColumn).NumberFormat = "mm/dd/yyyy, hh:mm:ss"
            Else
                Rng2.Offset(0, xOffsetColumn).ClearContents
            End If
        Next
        Application.EnableEvents = True
    End If

End Sub
Note: I did not analyze your code to see if it would do what you wanted, but I did clean up some errors you had in there in reference to your Range variables.
 
Upvote 0
Solution
Welcome to the Board!

Note that in order for the code to run automatically, it MUST be named "Worksheet_Change". You cannot add or remove anything from that name or else it will not run automatically.
And, you are only allowed on "Worksheet_Change" procedure per sheet. So you need to combine your two procedures together.

So you could combine them, something like this:
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)

    Dim WorkRng1 As Range
    Dim Rng1 As Range
    Dim xOffsetColumn As Integer
    Dim WorkRng2 As Range
    Dim Rng2 As Range

    xOffsetColumn = 3

'***FIRST BLOCK***
    Set WorkRng1 = Intersect(Application.ActiveSheet.Range("B:B"), Target)
    If Not WorkRng1 Is Nothing Then
        Application.EnableEvents = False
        For Each Rng1 In WorkRng1
            If Not VBA.IsEmpty(Rng1.Value) Then
                Rng1.Offset(0, xOffsetColumn).Value = Now
                Rng1.Offset(0, xOffsetColumn).NumberFormat = "mm/dd/yyyy, hh:mm:ss"
            Else
                Rng1.Offset(0, xOffsetColumn).ClearContents
            End If
        Next
        Application.EnableEvents = True
    End If

'***SECOND BLOCK***
    Set WorkRng2 = Intersect(Application.ActiveSheet.Range("G:G"), Target)
    If Not WorkRng2 Is Nothing Then
        Application.EnableEvents = False
        For Each Rng2 In WorkRng2
            If Not VBA.IsEmpty(Rng2.Value) Then
                Rng2.Offset(0, xOffsetColumn).Value = Now
                Rng2.Offset(0, xOffsetColumn).NumberFormat = "mm/dd/yyyy, hh:mm:ss"
            Else
                Rng2.Offset(0, xOffsetColumn).ClearContents
            End If
        Next
        Application.EnableEvents = True
    End If

End Sub
Note: I did not analyze your code to see if it would do what you wanted, but I did clean up some errors you had in there in reference to your Range variables.
You are wonderful. Thank you so much for your help.
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,175
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