View Single Post
  #3   Report Post  
Posted to microsoft.public.excel.programming
Norman Jones Norman Jones is offline
external usenet poster
 
Posts: 5,302
Default delete all blank rows in a spreadsheet

Hi Richard,

Try Something like:

'================
Public Sub Tester1()
Dim WB As Workbook
Dim SH As Worksheet
Dim Rng As Range
Dim rcell As Range
Dim delRng As Range
Dim CalcMode As Long
Dim ViewMode As Long

Set WB = Workbooks("A.xls") '<<===== CHANGE
Set SH = WB.Sheets("Sheet1") '<<===== CHANGE
Set Rng = SH.UsedRange

On Error GoTo XIT

With Application
CalcMode = .Calculation
.Calculation = xlCalculationManual
.ScreenUpdating = False
End With

With ActiveWindow
ViewMode = .View
.View = xlNormalView
End With

SH.DisplayPageBreaks = False

For Each rcell In Rng.Cells
If Application.CountA(rcell.EntireRow) = 0 Then
If delRng Is Nothing Then
Set delRng = rcell
Else
Set delRng = Union(rcell, delRng)
End If
End If
Next rcell

If Not delRng Is Nothing Then
delRng.EntireRow.Delete
End If

XIT:
With Application
.Calculation = CalcMode
.ScreenUpdating = True
End With

ActiveWindow.View = ViewMode

End Sub
'<<================


---
Regards,
Norman



"Richard" wrote in message
...
How do I delete entire rows in a spreadsheet with a VBA macro?

I can't use autofilter (or similar techniques) as not all of the columns
have data in every non blank row i.e. if I sorted by column A, there may
be a
row with a blank cell in column A, but another column (say AX) that may be
out of view could have data in it.