Using macro to delete all rows on a worksheet except first row, but first row is being deleted too

alkyze

New Member
Joined
Dec 11, 2020
Messages
12
Office Version
  1. 365
Platform
  1. Windows
I'm trying to delete all but the header row in a spreadsheet when a button is clicked. I'm using the code below which works perfectly except that it is deleting the header row as well.

Set ws = Worksheets ("Claims")
LastRow = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row)
ws.Range ("A2:A" & LastRow).EntireRow.Delete
 

Excel Facts

Workdays for a market open Mon, Wed, Friday?
Yes! Use "0101011" for the weekend argument in NETWORKDAYS.INTL or WORKDAY.INTL. The 7 digits start on Monday. 1 means it is a weekend.
That would happen if there are no rows of data on your sheet with data in column A except row 1 (so LastRow = 1).
Just add an IF statement, i.e.
VBA Code:
Set ws = Sheets("Claims")
LastRow = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row
If LastRow>1 Then ws.Rows("2:" & LastRow).Delete

Edit: I also cleaned up your code a little. You had some spaces/errors in it and was able to shorten a few of your lines of code.
 
Last edited:
Upvote 0
Solution
Thanks for your help! Definitely a brain dead moment for me
 
Upvote 0
No worries!
Glad I was able to help!
:)
 
Upvote 0

Forum statistics

Threads
1,214,590
Messages
6,120,421
Members
448,961
Latest member
nzskater

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