How do I delete blank rows?

kin

Well-known Member
Joined
Jun 26, 2007
Messages
648
Hi

I have used the code

Sub DeleteBlankRows()
Application.ScreenUpdating = False
Range("A2:D19962").SpecialCells(xlCellTypeBlanks).EntireRow.Delete
Application.ScreenUpdating = True
End Sub

But it deletes all rows even those with data. What is wrong?

thanks
 

Excel Facts

What is =ROMAN(40) in Excel?
The Roman numeral for 40 is XL. Bill "MrExcel" Jelen's 40th book was called MrExcel XL.
Difficult for me to suggest anything further without seeing your wb - I will send you a PM with my email address if you would like to send me a sample (with any sensitive or confidential data removed).
 
Upvote 0
The only other problem I can currently think of is if you are using Excel on a Mac rather than a PC? I've come across it before where the code for filtering blanks doesn't work in Mac 2004 VBA.
 
Upvote 0
Hi,
You might like to try
Code:
Sub delblankrow()
Dim n As Long, i As Long, x As Long
With ActiveSheet.UsedRange
n = .Rows.Count
For i = 1 To n
    If Not .Rows(i).Find("*") Is Nothing Then
        x = x + 1
        .Rows(i).Copy .Rows(x)
    End If
 Next i
Range(.Rows(x + 1), Rows(n)).ClearContents
End With
End Sub
Works fine on Excel 2004 for Mac. Works fine for Windows version also.

If you've got a lot of stuff you can disable screen updating by putting a line
Code:
application.screenupdating=0
after the dim line, but if not much data it shouldn't make much difference.
Also, as noted it may depend on how blank your "blank" rows really are.
 
Upvote 0
Minor editing of that code:
The line
Code:
Range(.Rows(x + 1), Rows(n)).ClearContents
should be
Code:
If x < n then Range(.Rows(x + 1), .Rows(n)).ClearContents
although I think usually unlikely to make much difference (note the period before second "Rows").
 
Upvote 0

Forum statistics

Threads
1,215,372
Messages
6,124,542
Members
449,169
Latest member
mm424

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