Macro to delete rows that have #N/A in a cell

Dazzawm

Well-known Member
Joined
Jan 24, 2011
Messages
3,786
Office Version
  1. 365
Platform
  1. Windows
I need a macro that will look at column G and any cell that has #N/A in it I want the entire row deleted. I need a macro because I want to add it on the end of an existing macro I have and sorting and deleting would not work because each file will have different amounts. Thanks.
 

Excel Facts

Which came first: VisiCalc or Lotus 1-2-3?
Dan Bricklin and Bob Frankston debuted VisiCalc in 1979 as a Visible Calculator. Lotus 1-2-3 debuted in the early 1980's, from Mitch Kapor.
You could record a macro using AutoFilter to trap the #N/A's then Goto-->Special-->Visible Cells Only-->Delete rows.

It'll be faster than trying to loop through.

HTH,
 
Upvote 0
You could record a macro using AutoFilter to trap the #N/A's then Goto-->Special-->Visible Cells Only-->Delete rows.

It'll be faster than trying to loop through.

HTH,

Gone a little over my head that one!!
 
Upvote 0
Whenever you are deleting rows, you always want to start at the bottom of your range. This can be rather slow though.

Code:
Sub Delete_Rows()
For r = 1000 to 1 Step -1  'THE ROWS THAT YOU WANT TO EXAMINE
    If Application.Worksheetfunction.Isna(Sheets("Sheet1").Range("G" & r).value) Then Sheets("Sheet1").Range("G" & r).EntireRow.Delete
Next r
End Sub
 
Upvote 0
Gone a little over my head that one!!

<font face=Calibri><SPAN style="color:#00007F">Sub</SPAN> Foo()<br>    <SPAN style="color:#00007F">Dim</SPAN> LR <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN><br>    LR = Cells(Rows.Count, "G").End(xlUp).Row<br>    <br>    Range("G2").AutoFilter<br>    <br>    <SPAN style="color:#00007F">With</SPAN> ActiveSheet.Range("$G$1:$G$" & LR)<br>            .AutoFilter Field:=1, Criteria1:="#N/A"<br>            .SpecialCells(xlCellTypeVisible).Select<br>            .Delete Shift:=xlUp<br>    <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">With</SPAN><br>        <br><SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Sub</SPAN></FONT>

HTH,
 
Upvote 0
The row amounts may vary in each file, anything up to a possible 20,000 rows.
 
Upvote 0
Sub Foo()
Dim LR As Long
LR = Cells(Rows.Count, "G").End(xlUp).Row

Range("G2").AutoFilter

With ActiveSheet.Range("$G$1:$G$" & LR)
.AutoFilter Field:=1, Criteria1:="#N/A"
.SpecialCells(xlCellTypeVisible).Select
.Delete Shift:=xlUp
End With

End Sub


HTH,

When added to the bottom of my macro it didn't get rid of them plus I lost my headers in row 1.
 
Upvote 0

Forum statistics

Threads
1,224,520
Messages
6,179,267
Members
452,902
Latest member
Knuddeluff

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