Delete rows if specific columns contain cells with "N/A"

lekh0602

New Member
Joined
Jul 9, 2014
Messages
33
Hi,

I would like to set up a button where cells with "N/A" in specific columns will then delete the relevant row - the columns are Z, AA and AB.

When I use the code below it does not seem to work.

The code is the following:

Sub Button3_Click()
'Declare variables
Dim rngFound As Range 'Used for the find loop
Dim rngDel As Range 'Used to store matching rows
Dim strFirst As String 'Used to store the first cell address of the find loop to prevent infinite loop


'Search for #N/A in column C
Set rngFound = Columns("Z").Find("#N/A", Cells(Rows.Count, "Z"), xlValues, xlWhole)


'Make sure something was found
If Not rngFound Is Nothing Then
'Found something, record first cell address
strFirst = rngFound.Address


'Start loop
Do
'Check if cells in column Z, AA, and AB are all #N/A
If Cells(rngFound.Row, "Z").Text = "#N/A" _
And Cells(rngFound.Row, "AA").Text = "#N/A" _
And Cells(rngFound.Row, "AB").Text = "#N/A" Then


'Found they are all #N/A, store the row in rngDel
If rngDel Is Nothing Then Set rngDel = rngFound Else Set rngDel = Union(rngDel, rngFound)


End If


'Advance the loop to the next cell with #N/A in column Z
Set rngFound = Columns("Z").Find("#N/A", rngFound, xlValues, xlWhole)


'Exit loop when back to the first cell
Loop While rngFound.Address <> strFirst
End If


'If rngDel has anything in it, delete all of its rows
If Not rngDel Is Nothing Then rngDel.EntireRow.Delete


'Object variable cleanup
Set rngFound = Nothing
Set rngDel = Nothing


End Sub

I would appreciate any inputs to what I am doing wrong.

Thanks.
 
Thanks a lot but it seems to still crash - do you think it is the amount of data that causes the problem?
 
Upvote 0

Excel Facts

Easy bullets in Excel
If you have a numeric keypad, press Alt+7 on numeric keypad to type a bullet in Excel.
Why try to filter / loop?

Code:
Sub Button3_Click()
    Columns("Z:AB").SpecialCells(xlCellTypeFormulas, 16).EntireRow.Delete
End Sub
 
Upvote 0
Why try to filter / loop?

Code:
Sub Button3_Click()
    Columns("Z:AB").SpecialCells(xlCellTypeFormulas, 16).EntireRow.Delete
End Sub
I agree with your suggestion, but just want to make a couple of comments. The "CellType" part of every argument name for SpecialCells can be omitted and the function will still work. So, for your suggested code, xlFormulas will work the same as xlCellTypeFormulas. Second, I think it makes the code clearer and more self-documenting if you use built-in variables rather than hard-coded "magic" numbers.... instead of 16, I would use the built-in constant xlErrors. Hence, my suggestion for your main code line would be to write it this way...

Columns("Z:AB").SpecialCells(xlFormulas, xlErrors).EntireRow.Delete
 
Upvote 0
Thanks Rick, I was having brainfreeze on the correct syntax so just grabbed the specialcells bit from the macro recorder.
 
Upvote 0
Thanks Rick, I was having brainfreeze on the correct syntax so just grabbed the specialcells bit from the macro recorder.

The macro recorder will give you the full, spelled-out main argument namewith the "CellType" part in it, but if you are typing the code out longhand, it is more convenient to omit that part of the text, hence my suggestion. Given that xlCellType@@@@ and xl@@@@ (where @@@@ is a stand-in for the various argument name types) are both defined the same, I don't understand why the longer version is even shown in the help files at all... I don't think including that "extra" text clarifies anything over and above the argument name which omits it.
 
Upvote 0
Hi Jason and Rick,

Thanks for your inputs. I just tried using Rick's suggestion and wrote the code like this:

Sub CMRDatenDSc_Button3_Click() 'Used for identifying cells with #N/A and then deleting the relevant rows


Columns("Z:AB").SpecialCells(xlFormulas, xlErrors).EntireRow.Delete


End Sub

However, I get a "Run-time error 1004 No cells were found". As I am new to VBA I can't figure out what I'm doing wrong. Would any of you know?

Thanks.
 
Upvote 0
I just tried using Rick's suggestion and wrote the code like this:

Sub CMRDatenDSc_Button3_Click() 'Used for identifying cells with #N/A and then deleting the relevant rows
How is the #N/A getting into your cells... as the result of a formula or are you typing it in?
 
Upvote 0
Yes, the #N/A is the result of formulas from column Y to AE - not all data necessary for the different formulas is available from another sheet. That's why I want to delete the rows where the cells show #N/A.
 
Upvote 0

Forum statistics

Threads
1,214,991
Messages
6,122,628
Members
449,095
Latest member
bsb1122

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