Deleting Entire Rows in Range

airforceone

Board Regular
Joined
Feb 14, 2022
Messages
201
Office Version
  1. 2019
  2. 2016
Platform
  1. Windows
needed help again....
what I'm trying to do is, loop thru the range (until last row) and delete entire rows if cell is empty/blank or cell value not equal to "PAID"
below code does not seem to work

VBA Code:
    LROW = 2
    Do While LROW <= LastRow
        If (IsEmpty(Cells(LROW, 4)) = True Or Not Cells(LROW, 3).Value Like "PAID") Then
            rows(LROW).Delete
        End If
        LROW = LROW + 1
    Loop

I already went on reading the following post to no avail...

 

Excel Facts

What is the last column in Excel?
Excel columns run from A to Z, AA to AZ, AAA to XFD. The last column is XFD.
If you are going to loop row-by-row to delete, best to start from the bottom and work up

However, still I would normally try to delete them all at once. If the data is not huge, try

VBA Code:
With Range("D1:D" & LastRow)
  .AutoFilter Field:=1, Criteria1:="", Operator:=xlOr, Criteria2:="PAID"
  .Offset(1).Resize(.Rows.Count - 1).EntireRow.Delete
  .AutoFilter
End With

If the data is huge and that code takes too long, post back for a faster (but longer) code.
 
Upvote 0
Solution
If you are going to loop row-by-row to delete, best to start from the bottom and work up

However, still I would normally try to delete them all at once. If the data is not huge, try

VBA Code:
With Range("D1:D" & LastRow)
  .AutoFilter Field:=1, Criteria1:="", Operator:=xlOr, Criteria2:="PAID"
  .Offset(1).Resize(.Rows.Count - 1).EntireRow.Delete
  .AutoFilter
End With

If the data is huge and that code takes too long, post back for a faster (but longer) code.
thanks again @Peter_SSs will merge code this afternoon and update you soon...
 
Upvote 0
@Peter_SSs truly your a great help! your code is good and it flies!
but please kindly indulge with me, what's the ".AutoFilter" do after deleting the rows?
 
Upvote 0
Glad it works well enough for you. Thanks for the confirmation. (y)

what's the ".AutoFilter" do after deleting the rows?
My code uses AutoFilter to hide all the rows that you want to keep and show the ones you want to delete.
It then deletes the visible rows.
If we did nothing else, the rows you want to keep would still be hidden. That line of code unhides them. :)
 
Upvote 0
Glad it works well enough for you. Thanks for the confirmation. (y)


My code uses AutoFilter to hide all the rows that you want to keep and show the ones you want to delete.
It then deletes the visible rows.
If we did nothing else, the rows you want to keep would still be hidden. That line of code unhides them. :)
big tango! will not be the last! cheers mate...
 
Upvote 0

Forum statistics

Threads
1,223,885
Messages
6,175,182
Members
452,615
Latest member
bogeys2birdies

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