VBA & Macro Problems

Correct Excel VBA Syntax for Multiple Noncontiguous Ranges

Chanuka GeekiyanageChanuka Geekiyanage Oct 21, 2026 868 views

Question details

The user is attempting to use VBA to create visual checkboxes across multiple noncontiguous ranges but experiences inconsistent behavior where incorrect cells are modified.

Correct Excel VBA Syntax for Multiple Noncontiguous Ranges
Product
Excel
Device & OS
not provided
Scenario
Using VBA and Wingdings 2 characters to create lightweight visual indicators (checkboxes) instead of slow built-in form controls across noncontiguous columns.
Observed behavior
When applying VBA code to multiple ranges combined in a single statement, the second range misbehaves, allowing both Yes and No boxes to be checked and placing characters in unintended cells.
Before you start

Ensure you have the Developer tab enabled in your spreadsheet and save a backup copy of your workbook before testing new VBA macro code.

Solution 1Recommended

Use Correctly Quoted Range Strings and Application.Intersect

Fix syntax errors by properly quoting the noncontiguous ranges and using Application.Intersect to isolate the target cell's behavior.

When handling a Worksheet_SelectionChange event, referencing multiple ranges as one combined object can cause relative cell offsets to behave unpredictably. Using Application.Intersect ensures the macro only triggers if the exact active cell falls within your defined boundaries.

1
Open the VBA Editor

Press Alt + F11 to open the Visual Basic for Applications editor, and double-click the specific worksheet in the Project Explorer where you want the checkboxes.

2
Define the noncontiguous range

Format your range string correctly with a comma separating the areas inside a single pair of quotes, for example: Me.Range("D407:E424,I407:J424").

3
Implement the Intersect method

Wrap your target validation using If Not Application.Intersect(Target, Me.Range("D407:E424,I407:J424")) Is Nothing Then. This guarantees the code only proceeds when the user clicks inside these specific areas.

4
Process the Yes/No logic based on column position

Instead of applying relative offsets to a combined range, check the specific Target.Column to clear the opposite checkbox. For example, if Target.Column = 4 (Column D), clear Column E. If Target.Column = 9 (Column I), clear Column J.

Use Correctly Quoted Range Strings and Application.Intersect
Best Practice: Always specify 'Target.Cells.Count = 1' at the beginning of your selection change event to prevent the macro from crashing if the user selects multiple cells at once.
Advanced Spreadsheet Editing with WPS

Manage Macros and VBA Seamlessly in WPS Office

WPS Spreadsheet provides excellent support for VBA and macros, allowing you to create custom visual checkboxes, debug code efficiently, and optimize large workbooks without performance drops.

  1. 1. Download and Install WPS Office: Get the latest version of WPS Office and open your macro-enabled spreadsheet (.xlsm).
  2. 2. Access the Developer Tools: Navigate to the Developer tab on the top ribbon and click on 'Visual Basic' to open the VBA Editor.
  3. 3. Edit the Range Syntax: Paste or modify your noncontiguous range macro code inside the worksheet object.
  4. 4. Save and Run: Save your progress and test the visual checkbox behavior natively within the WPS Spreadsheet interface.
Fully compatible with Microsoft Excel VBA syntax and macro formattingLightweight application ensures fast performance even with complex macrosBuilt-in developer tools for seamless code testing and debuggingFree alternative with a highly familiar user interface
microsoft office alternative - wps office

Frequently Asked Questions

Why do built-in form checkboxes slow down my Excel workbook?

Adding hundreds of individual shape objects (like form controls) drastically increases the file size and rendering load. Using VBA and Wingdings 2 font characters as visual indicators is a much lighter alternative that preserves performance.

How do I check if a cell is within multiple noncontiguous ranges in VBA?

You can use the Application.Intersect method. For example: `If Not Application.Intersect(Target, Range("A1:A10, C1:C10")) Is Nothing Then`. If it does not evaluate to Nothing, the active cell is inside the specified noncontiguous ranges.

Why does Me.Range behave unexpectedly when looping through multiple areas?

When you combine noncontiguous ranges, iterating blindly through `.Cells` or applying relative `.Offset` commands can cross into unmapped boundaries. It is safer to loop through `Range.Areas` or handle the intersect logic for the specific Target cell directly.