logo
search
VBA & Macro Problems

How to Run Excel VBA Macros Conditionally When a Checkbox is Selected

Partner EditorPartner Editor Oct 10, 2026 871 views

Question details

The user wants to execute specific VBA macros only when an Excel checkbox is selected, but the procedure exits or fails to run the sequence properly.

How to Run Excel VBA Macros Conditionally When a Checkbox is Selected
Product
Excel
Device & OS
not provided
Scenario
Automating spreadsheet workflows where the execution of a specific macro (like Macro 1a) depends on whether a user has checked a box on the sheet.
Observed behavior
The VBA procedure exits without running the conditional macro because the checkbox state is not correctly evaluated or the exact checkbox name is improperly referenced in the code.
Before you start

Ensure that the Developer tab is enabled in your Excel ribbon and verify whether you inserted a Form Control checkbox or an ActiveX checkbox, as the VBA syntax required to read their states is different.

Solution 1Recommended

Create a Conditional Test for the Checkbox State

Use an If...Then statement in VBA to accurately check the value of the checkbox before executing the specific macro sequence.

To conditionally run a macro, your VBA code must reference the checkbox's exact name and use the correct property to test its value. Form Controls use the value xlOn, while ActiveX controls evaluate to True.

1
Identify the checkbox name

Right-click the checkbox in your worksheet and look at the Name Box (to the left of the formula bar) to find its exact name, such as 'Check Box 1'.

2
Open the VBA Editor

Press Alt + F11 on your keyboard to open the Visual Basic for Applications Editor, and locate the module containing your macro.

3
Write the conditional statement

Insert an If statement. For a Form Control, use: If ActiveSheet.CheckBoxes("Check Box 1").Value = xlOn Then Call Macro1a. For an ActiveX control, use: If ActiveSheet.OLEObjects("CheckBox1").Object.Value = True Then Call Macro1a.

4
Continue the macro sequence

Add the call for your subsequent macros (e.g., Call Macro2) below the End If statement so the sequence continues properly after the condition is checked.

Create a Conditional Test for the Checkbox State
Important Naming Rule: Always replace placeholder names like 'Check Box 1' in the code snippet with the actual name of your checkbox to prevent the procedure from exiting unexpectedly.
WPS Office Automation

Automate Workflows Conditionally with WPS Spreadsheet

WPS Spreadsheet fully supports VBA (Visual Basic for Applications) macros and form controls. You can easily insert checkboxes and link them to conditional macro sequences just like in Microsoft Excel.

  1. 1. Open WPS Spreadsheet: Launch WPS Office and open your workbook in the Spreadsheet application.
  2. 2. Insert a checkbox: Navigate to the Developer tab, click on 'Insert', and select the Checkbox form control to add it to your sheet.
  3. 3. Access the VBA Editor: Click the 'Visual Basic' button on the Developer ribbon or press Alt + F11 to launch the editor.
  4. 4. Add conditional logic: Write your If...Then macro script referencing the newly created checkbox to control the execution sequence.
  5. 5. Save as macro-enabled: Save your document in the .xlsm format to ensure all VBA scripts are preserved and executed properly.
Full compatibility with Microsoft Excel VBA syntax, including Form Controls and ActiveX properties.Built-in VBA editor for seamless macro creation and debugging.Lightweight, fast-loading, and completely free alternative for advanced spreadsheet automation.
microsoft office alternative - wps office

Frequently Asked Questions

How do I find the actual name of my checkbox to use in the VBA code?

Right-click or Ctrl-click the checkbox to select it without triggering it. Once selected, look at the Name Box located immediately to the left of the formula bar. The text displayed there (e.g., 'Check Box 3') is the exact name you must use in your VBA script.

Why does my VBA macro exit without running the code inside the IF statement?

This usually happens because the condition evaluates to false. This can occur if you referenced the wrong checkbox name, used the wrong property (like comparing a Form Control to 'True' instead of 'xlOn'), or if the checkbox is situated on a different worksheet than the one currently active in the code.

What is the VBA syntax difference between an ActiveX checkbox and a Form Control checkbox?

Form Controls belong to the CheckBoxes collection and use numeric values for their state: xlOn (1) for checked and xlOff (-4146) for unchecked. ActiveX controls are referenced via OLEObjects (or directly by their codename) and use standard Boolean values: True for checked and False for unchecked.