Suspend Code When Row is Deleted

belmontry

New Member
Joined
Jun 28, 2011
Messages
2
I am running the following to automatically date stamp a range of data when anything in its respective row is modified:

Code:
Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range)
    If Intersect(Target, Range("A2:AA7000")) Is Nothing Then Exit Sub
    
    Application.EnableEvents = False
    Range("A" & Target.Row) = Date
    Application.EnableEvents = True
    
End Sub

However, I often need to delete entire rows from the sheet. In doing so, the code effectively date stamps the sequential row that was previously deleted, although I have not intentionally input any data. Example: If I want to delete a full row of data in Sheet Row C, I delete the selected Row C, but the subsequent new Row C (previously Row D) is date stamped since its contents were modified to the new Row C locations.

I don’t know what to do to prevent the date stamp from running if, and only if, a row of data is deleted from the spreadsheet.

Thanks
 

Excel Facts

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.
Hi,

Try:

Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    If Target.Count > 1 Then Exit Sub
    If Intersect(Target, Range("A2:AA7000")) Is Nothing Then Exit Sub
    
    Application.EnableEvents = False
    Range("A" & Target.Row) = Date
    Application.EnableEvents = True
    
End Sub

Assuming that you will only ever enter data into one cell at a time.
 
Upvote 0
Welcome to the board..

Try this as the first line in the code..

If Target.Columns.Count = Columns.Count Then Exit Sub

Hope that helps
 
Upvote 0

Forum statistics

Threads
1,223,711
Messages
6,174,025
Members
452,542
Latest member
Bricklin

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