How to Check for Blank Cells in an Excel VBA Range
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.

- 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.
Ensure you have the Developer tab enabled in your spreadsheet application and that your workbook is saved as a Macro-Enabled Workbook (.xlsm).
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.
Press ALT + F11 on your keyboard to open the Visual Basic for Applications (VBA) editor. From the top menu, click 'Insert' and select 'Module'.
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.
In your main Sub procedure, use an If statement to call the function directly. For example, type: If HasBlanks(UserXRange) Then.
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.

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. Download and Install: Download WPS Office Free from the official website and complete the quick installation process.
- 2. Open Your Spreadsheet: Launch WPS Spreadsheet and open your existing .xlsm file containing the VBA code.
- 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. Run the Macro: Paste your custom HasBlanks function into a module and run your macro without any compatibility issues.

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.




