Using Excel VBA to Count Blank Cells in a Range – 3 Methods

This dataset contains blank cells.

Dataset of excel vba count blank cells in range

 


Method 1 – Embed a VBA to Count Blank Cells in a Range with the COUNTIF Function

Steps:

  • Press Alt + F11 or go to Developer -> Visual Basic to open Visual Basic Editor.

  • In the code window, click Insert -> Module.

  • Enter the following code in the code window.
Sub CountBlankWithCountIfColumn()
    MsgBox WorksheetFunction.CountIf(Range("B4:B9"), "")
End Sub

 

excel vba count blank cells in range of column with COUNTIF

The code will count the blank cells in B4:B9.

  • Press F5 or click Run -> Run Sub/UserForm. You can also click the Run icon.

This is the output.

result of excel vba count blank cells in range or column with COUNTIF

A message box displays the total count of blank cells in the range: 1.

 

You can also count blank cells in a row with the COUNTIF function.

  • To calculate the blank cells in row 5, enter the code:
Sub CountBlankWithCountIfRow()
    MsgBox WorksheetFunction.CountIf(Range("B5:F5"), "")
End Sub

The code will count blank cells in B5:F5.

excel vba count blank cells in range of row with countif

  • Run the macro.

A message box displays the total count of blank cells in the range: 2.


Method 2 – Applying a Macro to Calculate Empty Cells with Excel’s COUNTBLANK Function

Steps:

  • Open the Visual Basic Editor in the Developer tab and Insert a Module.
  • Enter the following code.
Sub CountBlankWithCountBlankColumn()
  MsgBox WorksheetFunction.CountBlank(Range("B4:B9"))
End Sub

 

excel vba count blank cells in range of column with countblank

  • Run the code.

This is the output.

result of excel vba count blank cells in range of column with countblank

A message box displays the total count of blank cells in the range: 1.

You can also use the COUNTBLANK function. The code is similar, but you need to define the row range, instead of the column range.

  • To calculate the blank cells in row 5, enter the code:
Sub CountBlankWithCountBlankRow()
    MsgBox WorksheetFunction.CountBlank(Range("B5:F5"))
End Sub

The code will count blank cells in B5:F5.

excel vba count blank cells in range of row with countblank

  • Run the macro.

A message box displays the total count of blank cells in the range: 2.


Method 3 – Applying a VBA Macro to Compute Blank Cells in a Range in Excel

Steps:

  • Open the Visual Basic Editor in the Developer tab and Insert a Module.
  • Enter the following code.
Sub CountBlankCells()
Dim iRange As Range
Dim iInput As Range
Dim iCount As Long
On Error Resume Next
xTitleId = "Microsoft Excel"
Set iInput = Application.Selection
Set iInput = Application.InputBox("Select Range", xTitleId, iInput.Address, Type:=8)
For Each iRange In iInput
    If IsEmpty(iRange.Value) Then
        iCount = iCount + 1
    End If
Next
MsgBox "There are " & iCount & " blank cells in the range"
End Sub

 

VBA to count blank cells in range in Excel

  • Run the macro.
  • In the input box, select a range (here, B4:F9).
  • Click OK.

This is the output.

result of VBA to count blank cells in range in Excel

A message box displays the total count of blank cells in the range: 8.


Download Workbook

Download the free practice Excel workbook here.


<< Go Back to Count Cells | Formula List | Learn Excel

Get FREE Advanced Excel Exercises with Solutions!
Sanjida Ahmed
Sanjida Ahmed

Sanjida Ahmed, who graduated from Daffodil International University with a degree in Software Engineering, has worked with SOFTEKO since 2021. She has written over 100 articles on Excel & VBA and, since 2022, has worked as the Project Manager of the Excel Extension Development Project in the Software Development Department. Since starting this software development, she has established an outstanding workflow encompassing a full SDLC. She always tries to create a bridge between her skills and interests in... Read Full Bio

We will be happy to hear your thoughts

Leave a reply

Advanced Excel Exercises with Solutions PDF

 

 

ExcelDemy
Logo