VBA & Macro Problems

How to Check for Blank Cells in an Excel VBA Range

Kushani NimanthikaKushani Nimanthika Oct 26, 2026 869 views

Question details

The user needs to create a VBA Boolean function to check if a selected Excel range contains any blank cells before executing further code.

How to Check for Blank Cells in a VBA Range
Product
Excel / WPS Spreadsheet
Device & OS
not provided
Scenario
Validating user input ranges in macros to ensure no empty cells are present before processing the data.
Observed behavior
A VBA function is required that returns a True/False value indicating the presence of empty cells in a defined range without complex looping.
Before you start

Ensure you have the Developer tab enabled in your spreadsheet application and that your workbook is saved as a Macro-Enabled Workbook (.xlsm).

Solution 1Recommended

Use the CountBlank Function in a Custom VBA Boolean

The most efficient way to check for blank cells is by utilizing the built-in CountBlank worksheet function within a reusable custom VBA function.

This approach creates a clean, reusable function that returns True if blanks are found. It eliminates the need for loop-based checking, significantly speeding up macro execution on large data ranges.

1
Open the VBA Editor

Press ALT + F11 on your keyboard to open the Visual Basic for Applications (VBA) editor. From the top menu, click 'Insert' and select 'Module'.

2
Define the Boolean Function

In the new module window, type the function: Function HasBlanks(Rng) As Boolean. On the next line, input the logic: HasBlanks = WorksheetFunction.CountBlank(Rng) > 0. Finish with End Function.

3
Call the Function in Your Macro

In your main Sub procedure, use an If statement to call the function directly. For example, type: If HasBlanks(UserXRange) Then.

4
Add Warning Messages

Inside the If block, add a message box to alert the user and stop the macro if blanks are found. Type: MsgBox "Range cannot have blanks" followed by Exit Sub on the next line.

Use the CountBlank Function in a Custom VBA Boolean
Boolean Logic Note: Because the function returns True or False, explicitly comparing it with True (e.g., If HasBlanks = True Then) is unnecessary. Using the function directly inside the If statement is cleaner and highly recommended.
Efficient VBA Macro Support

Run VBA Macros Seamlessly in WPS Spreadsheet

WPS Office Spreadsheet provides excellent native support for VBA macros, allowing you to run, edit, and troubleshoot your custom scripts just as you would in Microsoft Excel.

  1. 1. Download and Install: Download WPS Office Free from the official website and complete the quick installation process.
  2. 2. Open Your Spreadsheet: Launch WPS Spreadsheet and open your existing .xlsm file containing the VBA code.
  3. 3. Access the Developer Tools: Navigate to the 'Developer' tab on the top ribbon and click on 'Visual Basic' to access the macro editor.
  4. 4. Run the Macro: Paste your custom HasBlanks function into a module and run your macro without any compatibility issues.
Fully compatible with Microsoft Excel macro-enabled formats (.xlsm, .xltm)Integrated Visual Basic Editor for seamless script creation and debuggingLightweight application that processes large data ranges and macros quicklyFree to download and use for your daily office tasks
microsoft office alternative - wps office

Frequently Asked Questions

Why is WorksheetFunction.CountBlank better than looping through cells in VBA?

Using a WorksheetFunction leverages the application's built-in calculation engine, which is heavily optimized. This is significantly faster and requires far less code than writing a VBA 'For Each' loop to inspect every individual cell in a large range.

Can I use this function for multiple different ranges at once?

Yes, because the range is passed as an argument to the function. You can call the exact same HasBlanks function multiple times in your code by simply feeding it different ranges, such as HasBlanks(UserXRange) and HasBlanks(UserYRange).

Does WPS Spreadsheet support the CountBlank worksheet function in VBA?

Yes, WPS Spreadsheet features a fully capable VBA environment that supports standard WorksheetFunction methods, including CountBlank, ensuring your Excel scripts work seamlessly.