Wednesday, October 7, 2009

Deleting blank rows and columns in excel

Option Explicit

Sub DeleteBlanks()

Columns("A:A").SpecialCells(xlCellTypeBlanks).EntireRow.Delete

End Sub
*****************************************************
Public Sub DeleteBlankRows()

Dim R As Long
Dim C As Range
Dim n As Long
Dim rng As Range

On Error GoTo skip
Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual

If Selection.Rows.Count > 1 Then
Set rng = Selection
Else
Set rng = ActiveSheet.UsedRange.Rows
End If
n = 0
For R = rng.Rows.Count To 1 Step -1
If Application.WorksheetFunction.CountA(rng.Rows(R).EntireRow) = 0 Then
rng.Rows(R).EntireRow.Delete
n = n + 1
End If
Next R

skip:
Application.ScreenUpdating = True
Application.Calculation = xlCalculationAutomatic
End Sub
*****************************************************

Sub DelEmptyCols()
' Deletes all empty columns on the active worksheet
Dim iCol As Integer
With ActiveSheet.UsedRange
For iCol = .Column + .Columns.Count - 1 To 1 Step -1
If IsEmpty(Cells(65536, iCol)) And IsEmpty(Cells(1, iCol)) Then
If Cells(65536, iCol).End(xlUp).Row = 1 Then Columns(iCol).Delete
End If
Next iCol
End With
End Sub

No comments:

Post a Comment