This script “works”, but only if I run it twice. Anyone have an idea of why this is happening? Dealing with the special types has been somewhat of a nightmare, I’m not sure if that’s just me or known issues. Using Excel 2010 on Windows 7. I’ve tried repeating the code twice as well to no avail. I tried putting this in a Do Until and ALWAYS get stuck in a forever loop the first time I execute. I’m not sure why executing it the second time seems to work
'Remove all Blank Cells
On Error Resume Next
For i = Cells.SpecialCells(xlCellTypeBlanks).Count To 1 Step -1
Cells.SpecialCells(xlCellTypeBlanks).EntireRow.Delete
Next i
If Cells.SpecialCells(xlCellTypeBlanks).Count = 0 Then
ActiveWorkbook.Close (True)
EDIT: Updated answer to show how deleting a “specialcells” range doesn’t reset the sheet’s
UsedRangeproperty, and how that can lead to problems.Try running this sub multiple times on a sheet with or without the call to
Activesheet.UsedRangecommented out…Saving and re-opening seems to also reset UsedRange…
EDIT2 you should be very careful using this! It deletes whole rows even if there are non-blank cells in that row. It will be OK for certain types of data layout, but not for others. Under certain circumstances you may also see an error “Cannot use that command on overlapping selection” when calling “delete”.