VBA date stamp fails when values are copied over

OlgaB

New Member
Joined
Jun 29, 2023
Messages
3
Office Version
  1. 365
Platform
  1. Windows
Hi all,
I use the below code to add/update date/user in my spreadsheet.
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)

Dim myTableRange As Range
Dim myDateTimeRange As Range
Dim myUpdatedRange As Range

'Your data table range
Set myTableRange = Range("j2:z1000000")

'Check if the changed cell is in the data tabe or not.
If Intersect(Target, myTableRange) Is Nothing Then Exit Sub

'Stop events from running
Application.EnableEvents = False

'Column for the date/time
Set myDateTimeRange = Range("AH" & Target.Row)
'Column for last updated date/time
Set myUpdatedRange = Range("AJ" & Target.Row)

'Determine if the input date/time should change
If myDateTimeRange.Value = "" Then

    myDateTimeRange.Value = Now
    myDateTimeRange.Offset(, 1).Value = Environ("username")

End If

'Update the updated date/time value
myUpdatedRange.Value = Now
myUpdatedRange.Offset(, 1).Value = Environ("username")


'Turn events back on
Application.EnableEvents = True
End Sub
However I have an issue when instead of updating the cells one by one the user copies value in, or just drags the previous value down.
Then the macros updates dates/user only for the first row in the changed range (in the attached example yes from the first row was dragged down)
Is there a way to fix it somehow?
Thanks a lot,
Olga.
 

Attachments

  • Capture.PNG
    Capture.PNG
    8.2 KB · Views: 3

Excel Facts

Can Excel fill bagel flavors?
You can teach Excel a new custom list. Type the list in cells, File, Options, Advanced, Edit Custom Lists, Import, OK
Loop through the changed cells range


Rich (BB code):
Private Sub Worksheet_Change(ByVal Target As Range)

Dim myTableRange As Range
Dim myDateTimeRange As Range
Dim myUpdatedRange As Range
Dim cell As Range

'Your data table range
Set myTableRange = Range("j2:z1000000")

'Check if the changed cell is in the data tabe or not.
If Intersect(Target, myTableRange) Is Nothing Then Exit Sub

'Stop events from running
Application.EnableEvents = False

For Each cell In Intersect(Target, myTableRange)

    'Column for the date/time
    Set myDateTimeRange = Range("AH" & cell.Row)
    'Column for last updated date/time
    Set myUpdatedRange = Range("AJ" & cell.Row)
   
    'Determine if the input date/time should change
    If myDateTimeRange.Value = "" Then
   
        myDateTimeRange.Value = Now
        myDateTimeRange.Offset(, 1).Value = Environ("username")
   
    End If
   
    'Update the updated date/time value
    myUpdatedRange.Value = Now
    myUpdatedRange.Offset(, 1).Value = Environ("username")

Next cell


'Turn events back on
Application.EnableEvents = True
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,091
Messages
6,123,062
Members
449,089
Latest member
ikke

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