Delete Blank Cells Or Entire Blank Rows Within A Range

Learn VBA Macros From Microsoft Excel Code Vault Snippets

What This VBA Code Does

Many times we as data analyzers receive data that is not so pretty (aka unorganized).  One thing that can help organize data into a neatly structured data set is to remove unnecessary blank cells or rows.  Below are two macros that can accomplish this task very efficiently. 

This first VBA macro code will delete all blank cells from your range.

Sub RemoveBlankCells()
'PURPOSE: Deletes single cells that are blank located inside a designated range
'SOURCE: www.TheSpreadsheetGuru.com

Dim rng As Range

'Store blank cells inside a variable
  On Error GoTo NoBlanksFound
    Set rng = Range("A1:A10").SpecialCells(xlCellTypeBlanks)
  On Error GoTo 0

'Delete blank cells and shift upward
  rng.Rows.Delete Shift:=xlShiftUp

Exit Sub

'ERROR HANLDER
NoBlanksFound:
  MsgBox "No Blank cells were found"

End Sub

If you want to delete the entire row instead of individual cells, you can use the following VBA macro code.

Sub RemoveBlankRows()
'PURPOSE: Deletes any row with blank cells located inside a designated range
'SOURCE: www.TheSpreadsheetGuru.com

Dim rng As Range

'Store blank cells inside a variable
  On Error GoTo NoBlanksFound
    Set rng = Range("A1:A10").SpecialCells(xlCellTypeBlanks)
  On Error GoTo 0

'Delete entire row of blank cells found
  rng.EntireRow.Delete
  
Exit Sub

'ERROR HANLDER
NoBlanksFound:
  MsgBox "No Blank cells were found"

End Sub


How Do I Modify This To Fit My Specific Needs?

Chances are this post did not give you the exact answer you were looking for. We all have different situations and it's impossible to account for every particular need one might have. That's why I want to share with you: My Guide to Getting the Solution to your Problems FAST! In this article, I explain the best strategies I have come up with over the years to getting quick answers to complex problems in Excel, PowerPoint, VBA, you name it

I highly recommend that you check this guide out before asking me or anyone else in the comments section to solve your specific problem. I can guarantee 9 times out of 10, one of my strategies will get you the answer(s) you are needing faster than it will take me to get back to you with a possible solution. I try my best to help everyone out, but sometimes I don't have time to fit everyone's questions in (there never seem to be quite enough hours in the day!).

I wish you the best of luck and I hope this tutorial gets you heading in the right direction!

Chris "Macro" Newman :)