logo
search
VBA & Macro Problems

How to Prevent Queued Start and Stop Button Clicks in VBA Forms

Jason MillerJason Miller Oct 14, 2026 869 views

Question details

The user needs to stop button clicks from queuing up and executing multiple times during a long-running VBA form process.

How to Prevent Queued Start and Stop Button Clicks in VBA Forms
Product
Spreadsheets VBA
Device & OS
not provided
Scenario
Running a long macro loop through a user form and trying to pause or stop it using a toggle button, which queues clicks instead of stopping immediately.
Observed behavior
Changing one button from Start to Stop does not process the click immediately. Clicks are queued and later trigger repeated start and stop actions.
Before you start

Before modifying your macro script, make sure to save a backup of your workbook. Long-running VBA loops can occasionally cause the application to freeze if an infinite loop is accidentally triggered during testing.

Solution 1Recommended

Use Separate Start and Stop Buttons with a Status Flag

The most reliable way to handle user interruptions in a long-running VBA process is to separate the controls, use a global flag, and allow the system to process background events during the loop.

When a single button acts as a toggle (changing its caption from Start to Stop), the system often queues the clicks because the macro thread is busy executing the loop. Once the loop finishes, the queued clicks are fired rapidly, causing unexpected behavior.

To resolve this, separate the functionality into two distinct buttons and use a flag variable combined with the DoEvents command to allow the form to listen for the Stop button click.

1
Create Separate Buttons

Open your UserForm in the VBA Editor. Remove the single toggle button and add two separate CommandButtons: name one 'cmdStart' (Start) and the other 'cmdStop' (Stop).

2
Declare a Global Stop Flag

At the very top of your UserForm code module (before any subroutines), declare a boolean variable: Public bStopProcess As Boolean. Alternatively, you can use a hidden text box on the form to hold the flag value.

3
Program the Stop Button

Double-click the Stop button and add code to change the flag state when clicked. For example: bStopProcess = True.

4
Update the Main Loop Logic

Inside the long-running loop attached to your Start button, add 'DoEvents' to force the application to yield and process UI clicks. Right after DoEvents, add a check: If bStopProcess = True Then Exit Do (or Exit For). Make sure to reset bStopProcess = False at the very beginning of the Start button code.

Use Separate Start and Stop Buttons with a Status Flag
DoEvents Performance Impact: Calling DoEvents in every single iteration of a fast, tight loop can significantly slow down your macro execution. Consider calling it only every 100 or 500 iterations using a Mod operator.
Advanced Spreadsheet Automation

Run VBA Macros Smoothly in WPS Office Spreadsheets

WPS Office fully supports VBA and macro execution, allowing you to build robust user forms, manage long-running processes, and easily assign dedicated start/stop functionality just like in Microsoft Excel.

  1. 1. Open your workbook: Launch WPS Spreadsheets and open your macro-enabled workbook.
  2. 2. Access the Developer Tools: Navigate to the Developer tab on the top ribbon and click on 'Visual Basic' to launch the integrated VBA Editor.
  3. 3. Edit the UserForm: Open your specific UserForm, drag and drop separate Start and Stop buttons from the toolbox, and apply the flag logic.
  4. 4. Run the Macro: Execute the form and easily halt processes mid-loop using your new, responsive Stop button.
Fully compatible with Microsoft Excel VBA macros (.xlsm and .xlsb formats).Lightweight architecture prevents heavy UI freezing during long-running automation tasks.Built-in VBA editor makes it easy to debug code and manage UserForm events.
QA img-9

Frequently Asked Questions

Why does my VBA form freeze during a long loop?

VBA runs sequentially on a single thread. When a long loop is executing, it blocks the application from updating the user interface or registering button clicks unless you explicitly tell it to yield control using the DoEvents command.

What is the DoEvents command and how does it help?

DoEvents is a built-in VBA function that temporarily pauses the macro execution. This pause allows the operating system to catch up and process other pending events, such as updating a progress bar or acknowledging a user clicking a Stop button.

Can I use a single toggle button instead of separate Start and Stop buttons?

While it is technically possible to change a button's caption and function dynamically, doing so during a tight loop often leads to click queuing and synchronization issues. Separate buttons provide cleaner event handling and prevent accidental double-clicks from queueing.