How to Disable Custom Ribbon Buttons While a VBA Macro Runs in Excel
Question details
The user needs to temporarily disable a custom ribbon button while a VBA macro is executing to prevent unwanted concurrent clicks or interference.

- Product
- Excel / WPS Spreadsheet
- Device & OS
- not provided
- Scenario
- Running a VBA callback or refresh operation where user interaction with custom ribbon controls needs to be restricted.
- Observed behavior
- Standard application properties like Application.ScreenUpdating and Application.EnableEvents do not prevent users from clicking custom ribbon buttons, and CommandButton.Visible does not apply to the ribbon.
Ensure you have access to the CustomUI XML of your workbook and have declared a global VBA variable to store the IRibbonUI object during the ribbon load event.
Use the getEnabled Callback and IRibbonUI.InvalidateControl
This is the standard approach to dynamically control the enabled state of custom ribbon elements during macro execution.
The CommandButton.Visible or Enabled properties apply exclusively to worksheet or UserForm controls, not to Ribbon controls. To control the custom Ribbon, you must use XML callbacks and invalidate the control so the application re-evaluates its state.
In your standard VBA module, declare a public boolean variable (e.g., Public bMacroRunning As Boolean) to track the macro's execution state.
Use the onLoad callback in your CustomUI XML to capture and store the IRibbonUI object into a global variable when the workbook opens.
In your CustomUI XML, add getEnabled="MyButton_GetEnabled" to your button control. In the VBA editor, write the corresponding callback function to return Not bMacroRunning.
At the start of your main macro, set bMacroRunning = True and call MyRibbon.InvalidateControl("YourButtonID"). At the end of the macro, set the variable to False and invalidate the control again to re-enable it.
Automate Workflows with WPS Spreadsheet VBA
WPS Spreadsheet provides robust support for VBA macros, enabling you to automate repetitive tasks, create custom functions, and manage workbook events efficiently with a familiar developer interface.
- 1. Enable Developer Tools: Open WPS Spreadsheet, navigate to the Options menu, and enable the Developer tab to access all Macro and VBA tools.
- 2. Open the VBA Editor: Click 'Visual Basic' on the Developer tab or simply press Alt + F11 to launch the integrated VBA development environment.
- 3. Write or Paste Macros: Insert a new module from the Insert menu and paste your macro code, including any global variables needed for managing ribbon or worksheet states.
- 4. Run Your Macro: Assign the macro to a keyboard shortcut or a standard worksheet button to execute your automated workflow instantly.

Frequently Asked Questions
Why doesn't Application.EnableEvents stop ribbon button clicks?
Application.EnableEvents only pauses worksheet and workbook-level events, such as SelectionChange or Workbook_Open. It does not affect the Ribbon UI, which is handled independently by the application's command bar framework.
Can I use CommandButton.Enabled = False for a custom ribbon button?
No. CommandButton properties are strictly for ActiveX controls placed on worksheets or UserForms. Custom Ribbon controls must be managed using XML callbacks like getEnabled.
What happens if I don't store the IRibbonUI object on load?
If the IRibbonUI object is not stored into a global variable during the XML's onLoad callback, you will not be able to call the InvalidateControl method later. This means your ribbon's state will remain static until the workbook is completely closed and reopened.




