Delete row loop amiss

Tigerexcel

Active Member
Joined
Mar 6, 2020
Messages
493
Office Version
  1. 365
  2. 2019
Platform
  1. Windows
This macro highlights the blanks within the range but doesn't actually delete any rows and I can't work out why. There's most likely more coding here than there needs to be so happy to take any feedback on that.

VBA Code:
Sub dellirow()
Dim rng As Range
Dim rw As Range
Dim usedrange As Range
Set rng = ActiveSheet.usedrange
rng.SpecialCells(xlCellTypeBlanks).Select
For Each rw In rng.Rows
    If WorksheetFunction.CountA(rng.EntireRow) = 0 Then
        rng.EntireRow.Delete
    End If
Next rw
End Sub
 

Excel Facts

Square and cube roots
The =SQRT(25) is a square root. For a cube root, use =125^(1/3). For a fourth root, use =625^(1/4).
See if this works:

VBA Code:
lr = ActiveSheet.UsedRange.Rows(ActiveSheet.UsedRange.Rows.Count).Row

For i = lr To 1 Step -1
    If WorksheetFunction.CountA(Rows(i)) = 0 Then Rows(i).Delete Shift:=xlUp
Next
 
Upvote 0
Here is different option for you which loops through all rows up to last found used row and uses UNION to collect empty rows
All empty rows are deleted together

VBA Code:
Sub dellirowTiger()
    Dim Del As Range, r As Long
    With ActiveSheet
        Set Del = .Cells(.Rows.Count, 1)
        For r = 1 To .Cells.Find(What:="*", After:=.Range("A1"), Lookat:=xlPart, LookIn:=xlFormulas, SearchOrder:=xlByRows, searchDirection:=xlPrevious).Row
            If WorksheetFunction.CountA(.Rows(r)) = 0 Then Set Del = Union(Del, .Cells(r, 1))
        Next r
    End With
    Del.EntireRow.Delete
End Sub
 
Upvote 0
Thanks Steve and Yongle, both versions are big improvements on mine.
Steve, you've got yours down to 3 lines which is pretty economical and it does the job. Yongle, I like the Union concept and I think I might be able to use it in another macro.
Does anyone know why my attempt failed?
 
Upvote 0
Crucial part with yours is that it was deleting from top to bottom on the sheet. You need to delete bottom to top or excel gets confused because the rows are effectively 'disappearing'. If you delete row 10 then row 9 is still row 9 going from bottom to top. If you delete row 10 then row 11 is now row 10 going from top to bottom. Yongles code will run faster.
 
Upvote 0
If you can use a single column to determine which rows to delete (here column A), then use this one-liner
VBA Code:
Range("A:A").SpecialCells(xlCellTypeBlanks).EntireRow.Delete

The above deletes EVERY row if column A does not contain a value
 
Upvote 0
Crucial part with yours is that it was deleting from top to bottom on the sheet. You need to delete bottom to top or excel gets confused because the rows are effectively 'disappearing'. If you delete row 10 then row 9 is still row 9 going from bottom to top. If you delete row 10 then row 11 is now row 10 going from top to bottom. Yongles code will run faster.
Thanks Steve, I'm going to have to ponder this for a while.
 
Upvote 0
If you can use a single column to determine which rows to delete (here column A), then use this one-liner
VBA Code:
Range("A:A").SpecialCells(xlCellTypeBlanks).EntireRow.Delete

The above deletes EVERY row if column A does not contain a value
Well surely no one can get quicker than that, it would take coding of less than 53 characters to do it.
 
Upvote 0

Forum statistics

Threads
1,215,477
Messages
6,125,031
Members
449,205
Latest member
Eggy66

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