Home Excel Powerpoint Word Outlook Error Codes

VBA Useful Codes

Welcome to the World of VBA
Home > VBAUsefulCodes

Delete Blank Rows and Sort Rows using VBA


This sample code snippet contain two functionalities. One is to delete blank row if there is no data though out the row in a selected range. Second one is to sort data using the specified column.

Sub DeleteBlankRows_Sort()
' Select the Range you want to remove blank rows and sort rows based on Column A
' Deletes the entire row within the selection if the ENTIRE row contains no data.

Dim i As Long

'turn off calculation and screenupdating to speed up the macro.

With Application
.Calculation = xlCalculationManual
.ScreenUpdating = False

'We work backwards because we are deleting rows.
For i = Selection.Rows.Count To 1 Step -1
If WorksheetFunction.CountA(Selection.Rows(i)) = 0 Then
Selection.Rows(i).EntireRow.Delete
End If
Next i

selected_rng = Selection.Address(0, 0)
' you can change the Column you want to sort
Range(selected_rng).Sort Key1:=Range("A1"), Header:=xlYes

.Calculation = xlCalculationAutomatic
.ScreenUpdating = True
End With


End Sub

Subscribe

Enter your e-mail below and get notified on the latest blog posts.




Tags

VBA Outlook KPI Excel Alt+F11 Dashboards Macros Recording Graphs Automation Developer WaterFall Powerpoint Charts Pivot Tables Forecast Charts


Follow Us