Remove Empty Rows and Columns

Automatically remove empty rows and columns from your data range

639 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

CSV Import with Data Types

Import CSV files with automatic data type detection and formatting

View Template
Beginner

Data Processing & Cleanup

Remove duplicates, clean data formats, and standardise entries

View Template
Intermediate

Timesheet & Attendance Tracker

Track employee working hours, overtime, absences, and holidays with automatic calculations, weekl...

View Template