Auto-Archive deleted Rows in Excel

liljunga

New Member
Joined
Feb 26, 2024
Messages
13
Office Version
  1. 365
Platform
  1. Windows
Hello, I'm trying to create a VBA script in Excel that moves deleted rows to an 'Archive' sheet when users right-click and delete entire rows. I've tried the following code, but it's not working as expected. When I right-click and delete rows, they are not moving to the 'Archive' sheet. Could someone please help me troubleshoot this issue? Or can someone provide me new code to test out?

Private Sub Workbook_SheetBeforeDelete(ByVal Sh As Object, ByVal Target As Range)
Dim wsArchive As Worksheet
Dim lastRow As Long

' Name of your Archive sheet
Set wsArchive = ThisWorkbook.Sheets("Archive")

' Determine the last row in the archive sheet
lastRow = wsArchive.Cells(wsArchive.Rows.Count, 1).End(xlUp).Row

' Copy the deleted rows to the archive sheet
Target.EntireRow.Copy wsArchive.Rows(lastRow + 1)

' Clear the deleted rows from the original sheet
Application.EnableEvents = False
Target.EntireRow.Delete
Application.EnableEvents = True
End Sub
 
As we spoke before, it doesn't know when somebody intends to insert or delete a row.

Create a macro button. Developer > Insert > ActiveX Command Button. Draw it on the screen. Right click and choose View Code. Add "ArchiveRow" in between the 2 lines.
Wonderful! Thanks for all the help Jeffrey!
 
Upvote 0

Excel Facts

Repeat Last Command
Pressing F4 adds dollar signs when editing a formula. When not editing, F4 repeats last command.

Forum statistics

Threads
1,223,886
Messages
6,175,198
Members
452,616
Latest member
intern444

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