Function for last modified date of cell

TheBlackYellowHat

New Member
Joined
Oct 4, 2022
Messages
2
Office Version
  1. 2016
Platform
  1. Windows
First and foremost, thank you to everyone on this forum. I've already found a few answers by just searching. But in this case I'm stumped.

So this is what I've done so far (I'm by no means an expert).. this works but breaks in a certain situation.

I've got this code in Sheet1 of my VBAProject:
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)

    If Intersect(Target, Range("O5:O300")) Is Nothing Then Exit Sub
    Target.Offset(0, -11) = Now()
    
End Sub

This checks if any cell in column O has been changed, and outputs todays date in the same row but in column D.

Like I said, it works, BUT, if I insert a new row anywhere beween O5 and O300, I get a runtime error 1004. Removing the row yields the same error.
Debugging points me to this line of code:
Code:
Target.Offset(0, -11) = Now()

This makes complete sense because that new row doesn't have any previous information so what's it checking? If I just click quit, then everything still works but it would be frustating to have an error pop up everytime a new row is added or deleted.

Sadly I have no idea how to figure out a workaround. So this is why I'm here. If anyone smarter and more experienced than me help, I would massively appreciate it! Thank you in advance to anyone who even reads this.
 

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.
Try this:

VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    If Target.Cells.CountLarge > 1 Then Exit Sub
        If Not Intersect(Target, Range("O5:O300")) Is Nothing Then
            Application.EnableEvents = False
            Target.Offset(0, -11) = Now()
            Application.EnableEvents = True
        End If
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,888
Messages
6,175,207
Members
452,618
Latest member
Tam84

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