Delete Row on Multiple Criteria not Working

Jl23

New Member
Joined
Feb 9, 2023
Messages
8
Office Version
  1. 365
Platform
  1. Windows
I'm trying do a simple code to delete rows if a condition in column B & column E are met, both need to be true to have the row deleted. Column B needs to have any text and Column E has to be blank. I've tried other versions of code I've found in examples trying to troubleshoot but I can't get anything to work with 'And'. If I switch out the And to Or I get things to work but then it is deleting rows I don't want deleted. Can anyone explain what might be causing it not to work? In image attached I would want the highlighted rows to be deleted only.

Sub delete_rows()

Dim row_count As Long
Dim i As Long
Dim ws As Worksheet

Set ws = ThisWorkbook.Sheets("Billing")

ws.Activate
row_count = ws.Cells(Rows.Count, "A").End(xlUp).Row

i = 5
Do While i <= row_count

If Cells(i, 2) = "*" _
And Cells(i, 5) = "" Then

Rows(i).EntireRow.Delete
i = i - 1
row_count = row_count - 1
End If

i = i + 1

Loop

End Sub
 

Attachments

  • Screenshot 2023-04-26 065418.png
    Screenshot 2023-04-26 065418.png
    33.6 KB · Views: 5
I should add this disclaimer that my explanation only pertains to For/Next loops where you are looping throw the rows.
In Do or While loops, you CAN more easily get caught in an infinite loop or delete the wrong rows (as Georgiboy mentioned), if you are not careful with how you have written it.
That is a good reason to avoid those type of loops in this situation.
 
Upvote 0

Excel Facts

Pivot Table Drill Down
Double-click any number in a pivot table to create a new report showing all detail rows that make up that number
I have not really seen that. It actually follows a logical pattern, and it is usually that is misses rows that should be deleted, if there are multiple consecutive rows that need deleting. That is because you are looping from low to high (if going forwards) while deleting rows shifts the rest of your range up (going from high to low).

This can be shown with a simple example. Let's say that you have three rows (1-3) and the first two should be deleted, but the third should not. So it looks something like this:
View attachment 90483

Now, let's look at what happens as we loop through forwards. The loop would be from 1 to 3.

Loop 1:
Looks at row 1, and determines that row 1 should be deleted.
So it deletes row 1 and shifts up the data like this:
View attachment 90484

Loop 2:
Looks at row 2, and determines that row 2 should NOT be deleted (as the other row we really want to delete has shifted up to row 1).
So our data set remains unchanged.

Loop 3:
Looks at row 3, which is blank/empty, so once again the data set remains unchanged.
So we are left with this at the end, where only 1 one of two rows that should have been deleted were actually deleted.
View attachment 90485

So you can see how consecutive rows to be deleted get missed, as the second in the consecutive row gets moved up to the row you just deleted (so this row gets passed over).
By looping through the range backwards, you are looping in sync (backwards) with how the data is shifting after deletion (moving up).
Thank you!!! I appreciate the explanation!
 
Upvote 0

Forum statistics

Threads
1,214,641
Messages
6,120,691
Members
448,978
Latest member
rrauni

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