[VBA] How to Bulk Delete Rows Containing Blank Cells | Using SpecialCells and EntireRow

目次

Introduction

When processing data in Excel, you often encounter situations where you need to “delete rows that contain blank cells.”

Deleting them manually one by one is time-consuming, but combining VBA’s SpecialCells and EntireRow allows you to process them efficiently in an instant.

This article explains how to bulk delete rows containing blank cells within a specified range, using practical code.

Code Example: Bulk Delete Rows Containing Blank Cells

Dim targetRange As Range

' 1. Explicitly specify the range to check
Set targetRange = Range("D2:F15")

' 2. Prevent error if no blank cells are found
On Error Resume Next

' 3. Delete entire rows containing blank cells
targetRange.SpecialCells(xlCellTypeBlanks).EntireRow.Delete

' Reset error handling
On Error GoTo 0

Explanation of the Code

1. Specifying the Range (Range("D2:F15"))

Explicitly specify the range you want to check for blanks. If any cell within this range is blank, the row containing it becomes a target for deletion.

2. Extracting Blank Cells (SpecialCells(xlCellTypeBlanks))

This method extracts only the blank cells within the range.

Note: If no blank cells exist in the range, this method throws an error. Therefore, On Error Resume Next is used to temporarily ignore errors.

3. Deleting the Row (.EntireRow.Delete)

This deletes the entire row that contains the blank cell. Since it targets the whole row, it works correctly even if the data spans other columns outside the specified range.

Important Notes

  1. Error Handling: Since SpecialCells(xlCellTypeBlanks) causes an error if there are no blanks, using On Error Resume Next is mandatory.
  2. No Undo: Row deletion cannot be undone via VBA. It is recommended to create a backup before running the macro.

Application: Hiding Rows Instead of Deleting

If you want to hide rows containing blank cells instead of deleting them, simply replace .Delete with .Hidden = True.

targetRange.SpecialCells(xlCellTypeBlanks).EntireRow.Hidden = True

Summary

In large datasets, “processing rows with blank cells in bulk” makes data cleaning and preprocessing very efficient.

ActionCode Example
Delete Rows.SpecialCells(xlCellTypeBlanks).EntireRow.Delete
Hide Rows.SpecialCells(xlCellTypeBlanks).EntireRow.Hidden = True
Error AvoidanceOn Error Resume NextOn Error GoTo 0

Please use this method to clear out unnecessary data rows and maintain a clean dataset.

よかったらシェアしてね!
  • URLをコピーしました!
  • URLをコピーしました!

この記事を書いた人

私が勉強したこと、実践したこと、してることを書いているブログです。
主に資産運用について書いていたのですが、
最近はプログラミングに興味があるので、今はそればっかりです。

目次