Date/Time Stamp - Macro Merge Assistance

DaRTH KiRo

New Member
Joined
Jan 24, 2018
Messages
39
Office Version
  1. 2016
Platform
  1. Windows
I need assistance merging the two macros below together. The goal is to have a date/time modified displayed in column K when data is entered in column J, as well as a date/time modified displayed in column M when data is entered in column L. I already have to two setup independent but am unsure how to make them work together.

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("J:J"), Target)
xOffsetColumn = 1
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


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("L:L"), Target)
xOffsetColumn = 1
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

Does the VLOOKUP table have to be sorted?
No! when you are using an exact match, the VLOOKUP table can be in any order. Best-selling items at the top is actually the best.
Welcome to the Board!

Since it seems that both procedures are doing the same thing, just on different ranges, try this:
Code:
Private Sub Worksheet_Change(ByVal Target As Range)

[COLOR=#ff0000]    Dim WorkRng1 As Range
    Dim WorkRng2 As Range[/COLOR]
    Dim WorkRng As Range
    Dim Rng As Range
    Dim xOffsetColumn As Integer
    
[COLOR=#ff0000]    Set WorkRng1 = Intersect(Application.ActiveSheet.Range("J:J"), Target)
    Set WorkRng2 = Intersect(Application.ActiveSheet.Range("L:L"), Target)
    
    Set WorkRng = Application.Union(WorkRng1, WorkRng2)[/COLOR]
    
    xOffsetColumn = 1
    
    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
 
Upvote 0
Thanks for your reply! :biggrin: This makes sense, we were thinking we wouldn't need it twice. However, after running this we receive a run-time error 5, invalid procedure call or argument. The issue appears to be stemming from the following line (this is what it highlights when I click Debug).

Set WorkRng = Application.Union(WorkRng1, WorkRng2)
 
Upvote 0
I see, it doesn't like to use empty ranges in the Union command. So we will need to check it like this:
Code:
Private Sub Worksheet_Change(ByVal Target As Range)

    Dim WorkRng1 As Range
    Dim WorkRng2 As Range
    Dim WorkRng As Range
    Dim Rng As Range
    Dim xOffsetColumn As Integer
    
    Set WorkRng1 = Intersect(Application.ActiveSheet.Range("J:J"), Target)
    Set WorkRng2 = Intersect(Application.ActiveSheet.Range("L:L"), Target)
    
    If Not WorkRng1 Is Nothing Then
        If Not WorkRng2 Is Nothing Then
            Set WorkRng = Application.Union(WorkRng1, WorkRng2)
        Else
            Set WorkRng = WorkRng1
        End If
    Else
        If Not WorkRng2 Is Nothing Then
            Set WorkRng = WorkRng2
        End If
    End If
    
    xOffsetColumn = 1
    
    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
 
Upvote 0
Solution

Forum statistics

Threads
1,223,243
Messages
6,170,964
Members
452,371
Latest member
Frana

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