Assign Macros in Excel: A Friendly Guide
Have you ever wished you could make your Excel workbooks more interactive and user-friendly? What if you could click a button, shape, or even an image...
Messy Excel files with random blank rows are frustrating. I’ve faced them too while working with reports from clients or raw exports from tools. Manually deleting rows one by one is boring and wastes time. That’s where a small VBA macro becomes a lifesaver. In this post, I’ll show you the basic macro, smarter upgrades, and easy adjustments so you can clean up your sheets in seconds.
Use this macro when:
Here’s the simplest version:
Sub DeleteBlankRows()
Dim RowNum As Long
Dim LastRow As Long
LastRow = ActiveSheet.Cells(Rows.Count, 1).End(xlUp).Row
For RowNum = LastRow To 1 Step -1
If WorksheetFunction.CountA(Rows(RowNum)) = 0 Then
Rows(RowNum).Delete
End If
Next RowNum
End Sub
How It Works
In short: Excel scans your sheet → finds empty rows → deletes them cleanly.
Sometimes the basic version is enough, but often you need more control. Let’s improve it step by step.
Instead of scanning the entire sheet, you may want to clean only a specific area:
Sub DeleteBlanksInSelection()
Dim Cell As Range
For Each Cell In Selection.Rows
If WorksheetFunction.CountA(Cell) = 0 Then
Cell.EntireRow.Delete
End If
Next Cell
End Sub
Just select the range first, then run the macro. Excel will delete blank rows only within that selection.
To avoid accidental deletions, let’s ask the user before running:
Sub DeleteBlankRowsConfirm()
Dim RowNum As Long
Dim LastRow As Long
If MsgBox("Delete all blank rows?", vbYesNo) = vbNo Then Exit Sub
LastRow = ActiveSheet.Cells(Rows.Count, 1).End(xlUp).Row
For RowNum = LastRow To 1 Step -1
If WorksheetFunction.CountA(Rows(RowNum)) = 0 Then
Rows(RowNum).Delete
End If
Next RowNum
End Sub
Now you’ll see a Yes/No box before the macro deletes anything.
Sometimes rows look blank but contain formulas or hidden characters. In such cases, you may want to check only one column (say column A):
Sub DeleteIfColumnABlank()
Dim RowNum As Long
Dim LastRow As Long
LastRow = ActiveSheet.Cells(Rows.Count, 1).End(xlUp).Row
For RowNum = LastRow To 1 Step -1
If Trim(Cells(RowNum, 1).Value) = "" Then
Rows(RowNum).Delete
End If
Next RowNum
End Sub
This looks only at column A. If column A is empty, the row is deleted.
Here are some small but useful edits you can make:
Replace ActiveSheet with a sheet name:
Sheets("Data").Cells(Rows.Count, 1).End(xlUp).Row
This keeps the structure but fills in a placeholder.
And that’s it! With just a few lines of VBA, you can wipe out messy blank rows in seconds.
We started with the basic macro, made it smarter with conditions and confirmations, and saw easy adjustments for specific needs. Try it out on a test file first, then use it in your daily work.
No comments yet. Be the first to comment!
Use Ctrl+T to convert your data range into a formatted table instantly.