Professional Documents
Culture Documents
COM/VBA/
Below are 6 methods that will delete rows from within a selection. If you know the range you can
replace "Selection" with your Range(). It is important to note that the least efficient methods involve
In some examples we turn off Calculation and Screenupdating. The reason we turn off calculation is in
case the range in which we are deleting rows contains lots of formulas, if it does Excel may need to
recalculate each time a row is deleted, slowing down the macro. The screenupdating being set to false
will also speed up our macro as Excel will not try to repaint the screen each time it changes.
Sub DeleteBlankRows1()
'Deletes the entire row within the selection if the ENTIRE row contains no
data.
'We use Long in case they have over 32,767 rows selected.
Dim i As Long
With Application
.Calculation = xlCalculationManual
.ScreenUpdating = False
If WorksheetFunction.CountA(Selection.Rows(i)) = 0 Then
Selection.Rows(i).EntireRow.Delete
End If
Next i
.Calculation = xlCalculationAutomatic
.ScreenUpdating = True
End With
End Sub
Sub DeleteBlankRows2()
Selection.EntireRow.SpecialCells(xlBlanks).EntireRow.Delete
On Error GoTo 0
End Sub
Sub DeleteBlankRows3()
Dim Rw As Range
If WorksheetFunction.CountA(Selection) = 0 Then
Exit Sub
End If
With Application
.Calculation = xlCalculationManual
.ScreenUpdating = False
Selection.SpecialCells(xlCellTypeBlanks).Select
If WorksheetFunction.CountA(Selection.EntireRow) = 0 Then
Selection.EntireRow.Delete
End If
Next Rw
.Calculation = xlCalculationAutomatic
.ScreenUpdating = True
End With
End Sub
Sub MoveBlankRowsToBottom()
With Selection
Orientation:=xlTopToBottom
End With
End Sub
Sub DeleteRowsBasedOnCriteria()
With ActiveSheet
.Range("A1").CurrentRegion.Offset(1, 0).SpecialCells _
(xlCellTypeVisible).EntireRow.Delete
.AutoFilterMode = False
End With
End Sub
Sub DeleteRowsWithSpecifiedData()
Columns(4).EntireColumn.Insert
.FormulaR1C1 = "=IF(RC[1]="""","""",IF(RC[1]=""Not
Needed"",NA()))"
.Value = .Value
.SpecialCells(xlCellTypeConstants,
xlErrors).EntireRow.Delete
End With
On Error GoTo 0
Columns(4).EntireColumn.Delete
End Sub
Open Excel.
Go to Insert>Module.
Push Alt+F8 and then select the macro name and click Run. Or select Options and assign a shortcut
key.
saying goes "Prevention is better than cure". The two examples below will remove blank rows as they
occur. Either code should be placed within the Worksheet module and will occur each time a cell
In both codes you will notice the Application.EnableEvents=False this is often needed within Event
codes like this, else the Event will be triggered again once the code executes which in turn will again
You will no doubt also notice the GoTo SelectionCode which occurs if the number of cells within the
selection exceeds one. The reason for this is an error would occur if the code reached the Target
The second example uses the Sort method rather than the EntireRow.Delete and is the preferred
method to use if possible. What happens is, any blank rows are placed at the bottom of the range
The use of the keyword Me is a good habit to get into when working within Worksheet and Workbook
modules. This was shown to me by my internet friend from Belgium, Geert Dumortier.
Application.EnableEvents = False
If WorksheetFunction.CountA(Target.EntireRow) = 0 Then
Target.EntireRow.Delete
End If
Application.EnableEvents = True
'Our code will only enter here if the selection is more than one cell.
Exit Sub
SelectionCode:
If WorksheetFunction.CountA(Selection.EntireRow) = 0 Then
Selection.EntireRow.Delete
End If
Application.EnableEvents = True
End Sub
Application.EnableEvents = False
Me.UsedRange.Sort Key1:=[A2],
Order1:=xlAscending, _
Header:=xlYes, OrderCustom:=1,
MatchCase:=False, _
Orientation:=xlTopToBottom
End If
Application.EnableEvents = True
Exit Sub 'Our code will only enter here if the selection is _
If WorksheetFunction.CountA(Selection.EntireRow) = 0 Then
Orientation:=xlTopToBottom
End If
Application.EnableEvents = True
End Sub