Customize the Excel Ribbon with VBA and XML
Question details
The user wants to add custom Excel Ribbon controls and connect them to VBA actions.

- Product
- Microsoft Excel
- Device & OS
- not provided
- Scenario
- Create a custom Ribbon tab, group, or button for an Excel workbook or add-in.
- Observed behavior
- The desired Ribbon controls appear in Excel and run VBA callback procedures when selected.
Save the workbook as an Excel Macro-Enabled Workbook (.xlsm) and install a Ribbon XML editor such as Office Custom UI Editor. Use macros only in files from trusted sources.
Add Ribbon XML and a VBA callback to an Excel workbook
Use this method to add a custom tab and button that runs VBA code when clicked.
The Ribbon is not exposed as a complete collection of VBA objects. Define the interface in Office Ribbon XML, then implement the named callbacks in VBA.
In Excel, select File > Save As and choose Excel Macro-Enabled Workbook (*.xlsm). Close the workbook before opening it in the XML editor.
Open the .xlsm file in Office Custom UI Editor. Select Insert > Office 2007 Custom UI Part, then enter this XML: <customUI xmlns="http://schemas.microsoft.com/office/2006/01/customui"><ribbon><tabs><tab id="customTab" label="My Tools"><group id="customGroup" label="Actions"><button id="runReportButton" label="Run Report" onAction="RunReport"/></group></tab></tabs></ribbon></customUI>
Use the editor's validation command to check the XML, then select File > Save and close the editor.
Reopen the workbook in Excel and open the Visual Basic Editor. Select Insert > Module and add: Public Sub RunReport(control As IRibbonControl) MsgBox "The custom Ribbon button works." End Sub
Save the workbook and enable macros if prompted and you trust the file. Select My Tools > Run Report to test the callback.

Work with Excel spreadsheets in WPS Office
WPS Office is a free, lightweight Office alternative with a familiar interface for opening and editing spreadsheets. It supports common Excel formats to help you move between applications, although Excel Ribbon XML and VBA customization may not work the same way in WPS.

Frequently Asked Questions
Can VBA alone create a custom Excel Ribbon tab?
No. Standard VBA does not provide a complete object model for creating Ribbon controls. Define the tab and controls in Ribbon XML, then use VBA procedures for their callbacks.
Why does my custom Ribbon button not run its macro?
Check that the button's onAction value matches a public VBA procedure name and that the procedure accepts an IRibbonControl parameter. Also check Excel's macro security prompt and enable macros only if you trust the workbook.
Where is the Ribbon XML stored?
The XML is embedded as a custom UI part inside the workbook or add-in file. Open the file in a Ribbon XML editor to view or change that part.
Can I use the same approach in an Excel add-in?
Yes. Embed the Ribbon XML in the add-in and place the matching public VBA callbacks in a standard module. Save and load the add-in in Excel before testing its controls.




