Remove Empty Rows and Columns
Automatically remove empty rows and columns from your data range
671 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.
Free AI generations every month — top up with credit packs anytime
Related Templates
More VBA templates in the same category
Intermediate
Timesheet & Attendance Tracker
Track employee working hours, overtime, absences, and holidays with automatic calculations, weekl...
View Template
Intermediate
Data Entry Form with Validation
Professional UserForm-based data entry system with field validation, dropdown lists, date pickers...
View Template
Advanced
Inventory Management System
Manage stock levels with automatic reorder alerts, stock movement logging, valuation reports, and...
View Template