Data Processing & Cleanup
Beginner
Remove Empty Rows and Columns
Automatically remove empty rows and columns from your data range
13 views
Perfect For:
- Clean up imported data
- Remove blank rows
- Compact data range
VBA Code
Sub RemoveEmptyRowsAndColumns()
Dim ws As Worksheet
Dim lastRow As Long, lastCol As Long
Dim i As Long, j As Long
Set ws = ActiveSheet
lastRow = ws.Cells(ws.Rows.Count, 1).End(xlUp).Row
lastCol = ws.Cells(1, ws.Columns.Count).End(xlToLeft).Column
' Remove empty rows (from bottom to top)
For i = lastRow To 2 Step -1
If Application.WorksheetFunction.CountA(ws.Rows(i)) = 0 Then
ws.Rows(i).Delete
End If
Next i
' Remove empty columns (from right to left)
For j = lastCol To 1 Step -1
If Application.WorksheetFunction.CountA(ws.Columns(j)) = 0 Then
ws.Columns(j).Delete
End If
Next j
MsgBox "Empty rows and columns removed!"
End Sub
Related Topics
cleanup
empty
rows
columns
Need Custom VBA Solutions?
Our AI-powered VBA generator can create custom code tailored to your specific requirements in seconds.
Generate Custom VBA CodeRelated Templates
More VBA templates in the same category
Beginner
Data Processing & Cleanup
Remove duplicates, clean data formats, and standardise entries
View Template