Macro to delete a row if #REF! appears

DilbertAsok

New Member
Joined
Feb 22, 2011
Messages
8
Hi,
I'm fighting with VBA to improve my existing macro.

My current (working) macro - basically copies the top row downwards for 3000 rows, then moves on to the next sheet.

Code:
Sub IMPORT()
    Sheets("Sheet1").Select
    Range("A1:I1").Select
    Selection.Copy
    Range("A2:A3000").Select
    ActiveSheet.Paste
    Application.CutCopyMode = False
    Sheets("Sheet2").Select
    Range("A1:I1").Select
    Selection.Copy
    Range("A2:A3000").Select
    ActiveSheet.Paste
    Application.CutCopyMode = False
    etc etc etc
End Sub

Which is fine, unless the top row of "Sheet1" contains an error (which can happen in cell B1 for example).

I want the macro to look at cell B1, if this is an error (will always be of the "#REF!" type), then delete row 1, LOOP back to look at B1, if this is an error, delete, LOOP back, until B1 isn't an error anymore, and then proceed with the macro above.

I've got this with the new code highlighted red, but it's not working...:

Code:
Sub IMPORT()
    Sheets("Sheet1").Select
[COLOR=red]    Do
    If IsError("B1") Then
    Selection.Cell("A1").EntireRow.Delete
    End If
    Loop Until IsError("B1") = False
[/COLOR]    Range("A1:I1").Select
    Selection.Copy
    Range("A2:A3000").Select
    ActiveSheet.Paste
    Application.CutCopyMode = False
    Sheets("Sheet2").Select
    etc etc etc
End Sub

It doesn't seem to delete the rows even the first time, nevermind the Loop back to check again.

Any offers?

Thanks,
 
And I'm done - thanks to all!

For the benefit of others, my final macro code looks like this:

Code:
    Sheets("Sheet1").Select
    Do
    If IsError(Range("B1")) Then Rows(1).Delete
    Loop Until IsError(Range("B1")) = False
    Range("A1:I1").Select
    Selection.Copy
    Range("A2:A3000").Select
    ActiveSheet.Paste
    Application.CutCopyMode = False
    ActiveSheet.Range("$A:$I").AutoFilter Field:=2, Criteria1:="0.00"
    Range("$A$2:$A$3000").SpecialCells(xlCellTypeVisible).EntireRow.Delete
    ActiveSheet.Range("$A:$I").AutoFilter Field:=2
    Selection.End(xlDown).Select
    Selection.EntireRow.Delete
    Range("A1").Select

    Sheets("Sheet2").Select
 
    etc etc

The penultimate 2 lines (beginning "Selection.End" and "Selection.EntireRow") are because I realised that each sheet would have 1 line of data at the bottom (a total) which shouldn't be included.

The final line (Range("A1").Select) is jsut taking me back to the top because I like to be tidy!

Now just wait for someone to change the goalposts and I'll be back again... :eeek:
 
Upvote 0

Excel Facts

Format cells as currency
Select range and press Ctrl+Shift+4 to format cells as currency. (Shift 4 is the $ sign).

Forum statistics

Threads
1,225,155
Messages
6,183,218
Members
453,152
Latest member
ChrisMd

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