VBA & Macro Problems

How to Run an Excel Macro from the Ribbon When the Workbook Is Closed

Phi Hung VoPhi Hung Vo Nov 2, 2026 869 views

Question details

The user needs to allow others to run a complex Excel macro directly from the Ribbon interface without requiring them to know the file's location or manually open the workbook containing the code.

How to Run an Excel Macro from the Ribbon When the Workbook Is Closed
Product
Excel
Device & OS
not provided
Scenario
Distributing a macro that performs data merging and email functions, requiring a seamless user experience where the tool is accessible at all times directly via the Office UI.
Observed behavior
The macro functions properly when the host workbook is manually opened, but lacks a dedicated Ribbon trigger that automatically loads the environment and executes the code when the workbook is initially closed.
Before you start

Before modifying Ribbon XML or saving your file as an add-in, verify that the Developer tab is enabled in Excel and make a dedicated backup copy of your original .xlsm macro workbook.

Solution 1Recommended

Convert to an XLAM Add-in with Custom Ribbon UI

Transforming your macro workbook into an Excel Add-in (.xlam) and injecting custom RibbonX XML is the most robust way to distribute macros seamlessly to other users.

An Excel Add-in (.xlam) loads silently in the background whenever Excel is launched. This means the macro is always available in memory without the user ever seeing the host workbook. By modifying the CustomUI XML, you can map specific macros to dedicated buttons on the Ribbon.

1
Prepare the VBA Callback

Open your macro workbook (.xlsm). In the VBA editor, modify your macro's signature so it can accept the Ribbon control. Change 'Sub YourMacro()' to 'Sub YourMacro(control As IRibbonControl)'.

2
Save as an Excel Add-in

Go to File > Save As, and choose 'Excel Add-in (*.xlam)' from the format dropdown. Close Excel completely after saving.

3
Add RibbonX Code via Editor

Download and open the Office RibbonX Editor. Drag and drop your newly created .xlam file into the editor. Go to Insert > Office 2010+ Custom UI Part.

4
Define the Custom Ribbon Button

In the new CustomUI part, paste the required XML to create a custom tab and button. Ensure the 'onAction' attribute perfectly matches the name of your VBA callback (e.g., onAction="YourMacro"). Save and close the editor.

5
Install the Add-in

Reopen Excel. Go to File > Options > Add-ins. Next to 'Manage: Excel Add-ins', click 'Go'. Click 'Browse', locate your .xlam file, check its box, and click OK. The new Ribbon tab will now appear and run your macro seamlessly.

Convert to an XLAM Add-in with Custom Ribbon UI
Tip: If you want users to review data before the macro runs, you can structure your callback to simply open a specific data workbook first, then require a second click on the Ribbon to execute the final mail merge and email sequence.
Free Microsoft Office alternative

Enjoy Powerful Spreadsheet Capabilities with WPS Office

If configuring complex CustomUI XML and RibbonX editors in Microsoft Excel is proving troublesome, consider trying WPS Office. It provides an exceptionally lightweight, user-friendly, and highly compatible alternative for handling robust spreadsheet tasks and everyday Office workflows without the steep learning curve.

  1. 1. Download the Installer: Visit the official WPS Office website and download the free installer for your operating system.
  2. 2. Install and Launch: Run the lightweight setup file. The installation takes only a few moments, after which you can immediately launch the suite.
  3. 3. Open Your Spreadsheets: Open your existing .xlsx or .xlsm files directly in WPS Spreadsheet to enjoy full compatibility and familiar features.
Seamless compatibility with Microsoft Excel formats (.xlsx, .xlsm, .xlam)Lightweight installation with incredibly fast software launch timesFamiliar tabbed interface making navigation effortlessCompletely free all-in-one suite with Spreadsheet, Writer, and Presentation
microsoft office alternative - wps office

Frequently Asked Questions

Can I run a VBA macro without opening Excel at all?

No, VBA requires the Excel application framework to execute. However, you can automate Excel to open silently in the background using VBScript or a batch file, allowing the macro to run without displaying the Excel graphical interface.

What is the Office RibbonX Editor?

The Office RibbonX Editor (formerly Custom UI Editor) is an open-source tool used by developers to view and edit the hidden XML components of Office files. It is strictly used to add custom tabs, groups, and buttons to the Office Ribbon interface.

Why do I get a 'Cannot run the macro' error when clicking my custom Ribbon button?

This error typically occurs if the 'onAction' attribute in your CustomUI XML does not exactly match the macro's name in your VBA module, or if the VBA macro lacks the required 'control As IRibbonControl' parameter in its signature.

Can I hide the workbook window instead of making it an add-in?

Yes. You can open the macro workbook normally, go to the View tab on the Ribbon, and click 'Hide'. When you save and close Excel, the workbook will remain hidden the next time it is opened, behaving similarly to an add-in.