HELP Please! Need VBA code to Delete all Rows

ktalone

New Member
Joined
Nov 22, 2013
Messages
3
For some reason I can't get this code to work. I have multiple sheets within this workbook and multiple sections where "#REF!" is found with data in between - for this reason I was told to start from the bottom up on each worksheet. I would like the code to look to column A and find any cell with 'REF!' listed and delete the whole row. I am very, very new to VBA code so I googled and found this code and can't seem to get it to work with '#REF!'. Any help would be greatly appreciated!! thank you very much!!

Sub Delete_Rows()
Dim i, LastRow
LastRow = Range("A" & Rows.Count).End(xlUp).Row
For i = LastRow To 1 Step -1
If Cells(i, "A").Value = "#REF!" Then
Cells(i, "A").EntireRow.Delete
End If
Next
End Sub


Katy
 

Excel Facts

Excel motto
Not everything I do at work revolves around Excel. Only the fun parts.
Hi Katy,

Try 'IsError(Cells(i,"A").Value)' instead of 'Cells(i, "A").Value = "#REF!"'.
Also use 'Rows(i).EntireRow.Delete' instead of 'Cells(i, "A").EntireRow.Delete'.

Does this help?

Paul
 
Upvote 0
Try this
Code:
Sub Delete_Rows()
Dim i, LastRow
LastRow = Range("A" & Rows.Count).End(xlUp).Row
on error resume next
With Range("A1:A" & LastRow).SpecialCells(xlCellTypeFormulas, xlErrors)
.Entirerow.delete
End with
On error goto 0
End Sub
 
Upvote 0
Figured it out! Just had to modify :)
Sub Delete_Rows()
'Deletes all rows with #REF! or error in column A
Dim i, LastRow
LastRow = Range("A" & Rows.Count).End(xlUp).Row
For i = LastRow To 1 Step -1
If IsError(Cells(i, 1).Value) Then
Cells(i, 1).EntireRow.Delete
End If
Next
End Sub
 
Upvote 0
Welcome to the Board!

Instead of looping, I'd use AutoFilter. If you filter on #REF!, then you can use Goto-->Special-->Visible Cells only and then delete the selected rows. It's a lot faster. It's also recordable.

HTH,
 
Upvote 0
The code I posted in Post #4 also avoids looping if you want to use code.
 
Upvote 0

Forum statistics

Threads
1,216,040
Messages
6,128,454
Members
449,455
Latest member
jesski

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