Search Contextures Sites
In addition to the default Excel Ribbon tabs, you can add your own tabs and buttons. In this tutorial you'll examine a sample file that creates a custom Ribbon tab with buttons that run simple Excel macros. Make a minor change to the Ribbon code, and test the result, using the Custom UI Editor, a free tool. Requires Excel 2007 or later version.
- Parts Database Example
- Video: Excel Ribbon Code Introduction
- View Ribbon Code with Custom UI Editor
- Reading the Custom UI Code
- Check the Ribbon Code
- Set Up the Macros
- Open the Parts Database File
- Test the Ribbon Buttons
- Download the Sample File
- Related Tutorials
Parts Database Example
This example is based on the parts database workbook, in which you enter data in a UserForm with drop down lists. The data is stored on a hidden sheet, and a pivot table summarizes the data in an Inventory report. You can learn how to set up the worksheets and the UserForm on this page: UserForm With ComboBoxes
In this tutorial, you'll see how to add a custom tab to the Excel Ribbon, instead of using worksheet buttons to run the macros.
Here is the Ribbon in Excel 2010, with a custom tab named Db Macros (short for database macros).
Watch this short video to see the steps for viewing and modifying Ribbon code in the Custom UI Editor tool. Then, in Excel, make a minor change to your macros, so they will run when a Ribbon button is clicked.
The written instructions are below the video.
The easiest way to view and edit the Ribbon code is to download and install the Custom UI Editor. This free program lets you see the existing code in a file, or create your own.
To see the Ribbon code in the Parts Database file:
- Download the sample Parts Database With Ribbon file
- Close the Parts Database file, and close Excel
- Install and open the Custom UI Editor
- Click the Open button on the menu bar.
- Locate and select the Parts Database file, and click Open
In the Custom UI Editor, you can see the workbook and the customUI.xml file at the left. The custom xlm code is shown at the right.
In the xml code, you can see that there is a heading for each part of the custom UI:
For each item that you create, you'll incude a unique ID, a label, and perhaps other properties, such as an icon and a macro that runs when the button is clicked.
For example, here is the code for the Hide Database button on the Db Macros tab.
- The ID -- DbG02B02 -- is a shortened version of Database tab, group 2, button 2. You could use a text only ID instead, if you prefer, as long as the ID is only used once in the code.
- The label -- Hide Database -- is the text that will appear on the button in the Ribbon tab.
- The image -- Lock -- will also appear on the button
- The size is set to Large, so it will be easy to see on the Ribbon.
- The onAction -- HideDatabase -- is the name of the macro that will run when the button is clicked.
You can also add a screen tip or super tip in the Ribbon.
- Screen tip: In the Parts Database, there is a screen tip for the Show Lists button, which is highlighted in yellow in the screen shot below.
- Super tip: Below the screen tip, circled in blue, is a super tip, which explains how to use the lists.
If you make changes the to the code, the Custom UI Editor can check the revised code, to make sure that it is valid. For example:
- In the first group, change the second button's ID from DbG01B02 to DbG01B01
- On the Custom UI menu bar, click the Validate button
- An error message appears, warning that the ID is a duplicate.
- Click OK, change the button ID back to DbG01B02 and click the Validate button again. A different message appears, announcing the the code is well-formed.
- Click OK, to close the message
- Click the Save button, to save your changes.
If your Ribbon buttons will run macros, you'll need to add a Ribbon callback in each macro's arguments. To see how these are set up, you can use another command in the Custom UI Editor.
- On the Custom UI Editor's menu, click the Generate Callbacks button.
- A new code sheet appears, with all the macros listed as onAction items in your Custom UI code.
- In the brackets after each macro name, you can see the ribbon callback: (control As IRibbonControl)
- You could copy this code, and paste it into the Visual Basic Editor in Excel, and fill in the code for each callback. Or, modify each of your existing macros, to add that ribbon callback.
- To return to the Custom UI code window, click the workbook name at the left of the Custom UI Editor.
After you have made the Custom UI changes, you can close the Custom UI Editor, and test the file in Excel.
- Save your changes, and close the Custom UI Editor.
- Open Excel, and open the Parts Database file.
- If a security warning appears, click the Enable Content button, so the macros will run.
- To see the VBA code, press Alt + F11, and view the code in the modNavigate module. You'll see that each macro has the Ribbon Callback added.
For an explanation of the UserForm that is in the workbook, and its code, please see this page: UserForm With ComboBoxes
Now that everything is set up, you can test the buttons on the Db Macros Ribbon tab.
- Close the Visual Basic Editor, and return to Excel.
- To test the custom tab items, click on the Db Macros tab, and click one of the buttons. For example, click the Show Database button, to see the hidden database sheet.
Note: This button does not have a screen tip or super tip, so its label shows up as a screen tip -- Show Database.
Download the sample file -- Parts Database With Custom Ribbon Tab. The zipped file is in Excel 2007 / 2010 format, and contains macros.
Thanks to Ron de Bruin, Excel MVP, for the helpful Ribbon customization information on his website, and thanks to Stephen Davanzo, whose sample workbook inspired me to start experimenting with the Ribbon.
Last updated: May 30, 2016 10:49 AM
Contextures Inc., Copyright ©2016
All rights reserved.