logo
search
VBA & Macro Problems

How to Disable Custom Ribbon Buttons While a VBA Macro Runs in Excel

Rana GarciaRana Garcia Oct 16, 2026 869 views

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.

How to Disable a Custom Ribbon Button While a VBA Macro Runs
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.
Before you start

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.

Solution 1Recommended

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.

1
Define a global state variable

In your standard VBA module, declare a public boolean variable (e.g., Public bMacroRunning As Boolean) to track the macro's execution state.

2
Capture the Ribbon UI object

Use the onLoad callback in your CustomUI XML to capture and store the IRibbonUI object into a global variable when the workbook opens.

3
Set up the getEnabled callback

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.

4
Invalidate control during macro execution

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.

Modifying Ribbon XML: You will need a specialized tool like the Custom UI Editor or Office RibbonX Editor to inject the required XML into your workbook file before writing the VBA callbacks.

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. 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. 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. 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. 4. Run Your Macro: Assign the macro to a keyboard shortcut or a standard worksheet button to execute your automated workflow instantly.
Fully compatible with Microsoft Excel VBA syntax and logicSupports custom forms, module insertions, and macro recordingLightweight application with fast macro execution performanceSeamless file migration without losing macro functionality
microsoft office alternative - wps office

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.