Delete entire row based on multiple cell criteria

NKlawender

New Member
Joined
Apr 5, 2022
Messages
21
Office Version
  1. 365
Platform
  1. Windows
I am trying to write a macro that will delete rows based on criteria in two columns. The columns contain written answers to survey questions. I want to delete the rows where both cells are blank, both cells say <Not defined>, both cells say na or n/a, both cells have just a period. Here is what I have written. The issue is that I get an error every time i try to run it, when I hit debug, the part in yellow is what is highlighted. I'm not sure what the issue is. Also, is there another way this could be done that may be faster? There are usually around 35,000 rows of data.

'Delete blank verbatim response rows
Dim LastA As Long
With Sheets("Combined Clean Files")
LastA = .Range("A" & Rows.Count).End(xlUp).Row
Application.ScreenUpdating = False
For r = LastA To 2 Step -1
If .Cells(r, 11) = "" And .Cells(r, 12) = "" Then .Cells(r, "A").EntireRow.Delete
If .Cells(r, 11) = "." And .Cells(r, 12) = "." Then .Cells(r, "A").EntireRow.Delete
If .Cells(r, 11) = "na" And .Cells(r, 12) = "na" Then .Cells(r, "A").EntireRow.Delete
If .Cells(r, 11) = "n/a" And .Cells(r, 12) = "n/a" Then .Cells(r, "A").EntireRow.Delete
If .Cells(r, 11) = "<Not defined>" And .Cells(r, 12) = "<Not defined>" Then .Cells(r, "A").EntireRow.Delete
Next r
End With
Application.ScreenUpdating = True


There is a little more to this code, but it is only opening and saving the files.
Thank you,
Norm
 

Excel Facts

Difference between two dates
Secret function! Use =DATEDIF(A2,B2,"Y")&" years"&=DATEDIF(A2,B2,"YM")&" months"&=DATEDIF(A2,B2,"MD")&" days"
I am trying to write a macro that will delete rows based on criteria in two columns. The columns contain written answers to survey questions. I want to delete the rows where both cells are blank, both cells say <Not defined>, both cells say na or n/a, both cells have just a period. Here is what I have written. The issue is that I get an error every time i try to run it, when I hit debug, the part in yellow is what is highlighted. I'm not sure what the issue is. Also, is there another way this could be done that may be faster? There are usually around 35,000 rows of data.

'Delete blank verbatim response rows
Dim LastA As Long
With Sheets("Combined Clean Files")
LastA = .Range("A" & Rows.Count).End(xlUp).Row
Application.ScreenUpdating = False
For r = LastA To 2 Step -1
If .Cells(r, 11) = "" And .Cells(r, 12) = "" Then .Cells(r, "A").EntireRow.Delete
If .Cells(r, 11) = "." And .Cells(r, 12) = "." Then .Cells(r, "A").EntireRow.Delete
If .Cells(r, 11) = "na" And .Cells(r, 12) = "na" Then .Cells(r, "A").EntireRow.Delete
If .Cells(r, 11) = "n/a" And .Cells(r, 12) = "n/a" Then .Cells(r, "A").EntireRow.Delete
If .Cells(r, 11) = "<Not defined>" And .Cells(r, 12) = "<Not defined>" Then .Cells(r, "A").EntireRow.Delete
Next r
End With
Application.ScreenUpdating = True


There is a little more to this code, but it is only opening and saving the files.
Thank you,
Norm

Hi. What part highlights during debug? There's nothing highlighted in what you provided.

Also, you should put code in the VBA wrap. See below example.

VBA Code:
'Delete blank verbatim response rows
Dim LastA As Long
With Sheets("Combined Clean Files")
LastA = .Range("A" & Rows.Count).End(xlUp).Row
Application.ScreenUpdating = False
    For r = LastA To 2 Step -1
        If .Cells(r, 11) = "" And .Cells(r, 12) = "" Then .Cells(r, "A").EntireRow.Delete
        If .Cells(r, 11) = "." And .Cells(r, 12) = "." Then .Cells(r, "A").EntireRow.Delete
        If .Cells(r, 11) = "na" And .Cells(r, 12) = "na" Then .Cells(r, "A").EntireRow.Delete
        If .Cells(r, 11) = "n/a" And .Cells(r, 12) = "n/a" Then .Cells(r, "A").EntireRow.Delete
        If .Cells(r, 11) = "<Not defined>" And .Cells(r, 12) = "<Not defined>" Then .Cells(r, "A").EntireRow.Delete
    Next r
End With
Application.ScreenUpdating = True
 
Upvote 0
Sorry, I am having trouble with the add-in on my work computer. The part that is highlighting is the first If Then line.
I had this problem once before, but never found a solution.
Thanks,
Norm
 
Upvote 0
Any idea if there would be a non-looping solution to this? I am still very new to VBA and am learning a ton from this and other sites.
Thanks
 
Upvote 0
Sorry, I am having trouble with the add-in on my work computer. The part that is highlighting is the first If Then line.
I had this problem once before, but never found a solution.
Thanks,
Norm

I received the below when I tested on a protected worksheet. Is your sheet password protected? If so, you'll have to .unprotect it before the loop and apply the .protect again after the loop.

If that isn't the issue, can you send the specific debug error you receive?

1660253456873.png
 
Upvote 0
I received the below when I tested on a protected worksheet. Is your sheet password protected? If so, you'll have to .unprotect it before the loop and apply the .protect again after the loop.

If that isn't the issue, can you send the specific debug error you receive?

View attachment 71392
The workbook is not password protected.
1660253736729.png


1660253773479.png
 
Upvote 0
Do you have an cells with "#N/A". I tried adding a few of those to my test sheet and it gave me a runtime 13 error.

If you do have "#N/A" cells, I was able to replace them all with blanks prior to starting your loop. See below.

VBA Code:
Sub testdeleterows()

'Delete blank verbatim response rows
Dim LastA As Long
With Sheets("Combined Clean Files")

.Cells.Replace "#N/A", "" 'Added to remove error cells

LastA = .Range("A" & Rows.Count).End(xlUp).Row
Application.ScreenUpdating = False
    
    For r = LastA To 2 Step -1
          If .Cells(r, 11) = "" And .Cells(r, 12) = "" Then .Cells(r, "A").EntireRow.Delete
          If .Cells(r, 11) = "." And .Cells(r, 12) = "." Then .Cells(r, "A").EntireRow.Delete
          If .Cells(r, 11) = "na" And .Cells(r, 12) = "na" Then .Cells(r, "A").EntireRow.Delete
          If .Cells(r, 11) = "n/a" And .Cells(r, 12) = "n/a" Then .Cells(r, "A").EntireRow.Delete
          If .Cells(r, 11) = "<Not defined>" And .Cells(r, 12) = "<Not defined>" Then .Cells(r, "A").EntireRow.Delete
    Next r
End With
Application.ScreenUpdating = True

End Sub
 
Upvote 0
Solution
Do you have an cells with "#N/A". I tried adding a few of those to my test sheet and it gave me a runtime 13 error.

If you do have "#N/A" cells, I was able to replace them all with blanks prior to starting your loop. See below.

VBA Code:
Sub testdeleterows()

'Delete blank verbatim response rows
Dim LastA As Long
With Sheets("Combined Clean Files")

.Cells.Replace "#N/A", "" 'Added to remove error cells

LastA = .Range("A" & Rows.Count).End(xlUp).Row
Application.ScreenUpdating = False
   
    For r = LastA To 2 Step -1
          If .Cells(r, 11) = "" And .Cells(r, 12) = "" Then .Cells(r, "A").EntireRow.Delete
          If .Cells(r, 11) = "." And .Cells(r, 12) = "." Then .Cells(r, "A").EntireRow.Delete
          If .Cells(r, 11) = "na" And .Cells(r, 12) = "na" Then .Cells(r, "A").EntireRow.Delete
          If .Cells(r, 11) = "n/a" And .Cells(r, 12) = "n/a" Then .Cells(r, "A").EntireRow.Delete
          If .Cells(r, 11) = "<Not defined>" And .Cells(r, 12) = "<Not defined>" Then .Cells(r, "A").EntireRow.Delete
    Next r
End With
Application.ScreenUpdating = True

End Sub
That seemed to do it. Thank you!
 
Upvote 0

Forum statistics

Threads
1,215,059
Messages
6,122,918
Members
449,094
Latest member
teemeren

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