When deleting data (rows) macros prompts an error and cannot be used anymore

cherryk0317

New Member
Joined
Mar 20, 2024
Messages
1
Office Version
  1. 365
Platform
  1. Windows
Private Sub CommandButton2_Click()

Range("A5").Select
Selection.End(xlDown).Select
Selection.Offset(1, 0).Select

Sheets("Add Entry").Select

Application.GoTo Reference:="R6C4"
ActiveCell.FormulaR1C1 = Now()

End Sub


currently out file has 900 plus entries and I want to delete the previous data on the file. How do I delete the data without the macro being affected. Deleting the rows prompts that this line as an error.
 

Excel Facts

How to calculate loan payments in Excel?
Use the PMT function: =PMT(5%/12,60,-25000) is for a $25,000 loan, 5% annual interest, 60 month loan.
The error is most likely caused by xlDown. When you xlDown, you're probably going to the end of the spreadsheet row 1048576. at the end, you cannot go down one more cell, therefore, the error.

try this, with an error handler:

Private Sub CommandButton2_Click()

Application.Run "macro1"
Application.Run "macro2"

End Sub

Sub Macro1()
On Error GoTo Err_Handler
For i = 1 To 6489
Range("A5").Select
Selection.End(xlDown).Select
Selection.Offset(1, 0).Select
Next

Go_To_A1:
Application.GoTo Reference:="R1C1"
Application.Calculation = xlAutomatic

Err_Handler:
Application.GoTo Reference:="R1C1"
Application.Calculation = xlAutomatic
End Sub


Sub Macro2()
Sheets("Add Entry").Select
Application.GoTo Reference:="R6C4"
ActiveCell.FormulaR1C1 = Now()
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,170
Members
453,021
Latest member
Justyna P

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