In a previous post, I went over how to modify the Excel ribbon with some basic buttons. This time, I’m going to focus specifically on a toggle button that can change the value of a cell or trigger a calculation or macro. In this example, I’ll show you how you can create a toggle button on the ribbon that will change the color of the sheet, switching between having no fill and a white fill.
The first thing you’ll want to do is download the CustomUI Editor, which you should be able to find online through a search. Once you have that, you can get to work on coding the xml.
Creating the ribbon code
To get started, you can refer to this post on how to create a simple tab and button on the Excel ribbon. Once you have that basic framework set up in the CustomUI Editor, it’s just a matter of modifying the type of button you’ll use. In that post, I just inserted some generic code for a button:
The button in that example is just a basic button. To turn it into a toggle button, I just need to modify the syntax so rather than saying ‘button’ it says ‘toggleButton’. I still need a label, an image, and specify what happens when it is pressed (the ‘onAction’ argument). Here’s what my updated xml code looks like:
Changing button to toggleButton just allows the button to have a pressed state so that when you click on it, it can look sunken. You can use that variable to also dictate what action should be taken. The key to making the toggle button work is to attach some macros to it, which is what I’ll cover next.
Creating the callback macros
A callback macro tells Excel what should happen when the toggle button is clicked. Here’s what the macro should look like initially:
Sub Callback(control As IRibbonControl, pressed As Boolean)
End Sub
The pressed value is a boolean, so it will only either be true or false. The next part of the code involves setting up an if statement to check on that value.
If pressed = True Then
'code goes here if the value is true (pressed)
Else
'code goes here if the value is false (not pressed)
End If
In this example, I’m going to make a simple macro where the sheet is either filled white, or there is no fill effect. The code isn’t complicated and I will start will selecting all the cells, selecting the interior property, and setting it to solid (i.e. ‘filled’):
Cells.Interior.Pattern = xlSolid
And to change it back so there is no fill effect:
Cells.Interior.Pattern = xlNone
Within the full callback macro, this is how all the code looks:
Sub Callback(control As IRibbonControl, pressed As Boolean)
If pressed = True Then
Cells.Interior.Pattern = xlSolid
Else
Cells.Interior.Pattern = xlNone
End If
End Sub
Here is the toggle button in action:
If you liked this post on How to Create a Toggle Button on the Excel Ribbon, please give this site a like on Facebook and also be sure to check out some of the many templates that we have available for download. You can also follow us on Twitter and YouTube.
In a previous post, I covered how you can customize the Excel ribbon in what was a pretty manual process. The good news is there is a much easier way to make changes to the ribbon using the Custom UI Editor. Below, I’ll cover how you can add and remove both tabs and groups, how to add buttons, and how to even use your own images. The first thing you’ll want to do is to download the Custom UI Editor. There’s not a definitive place you can always find this tool at so your best bet is to do a Google search.
How the Custom UI Editor works
Once you have downloaded the Custom UI Editor, you can get to work and begin to customize the Excel ribbon. Unlike just a simple customization, when you modify the ribbon using the Custom UI Editor, you are making changes to the Excel file itself. That means when you send the file to someone, they will see the changes you have made. With just a ribbon customization, those changes only apply to your computer. But these changes will be saved within the file. The Custom UI Editor goes through the cumbersome process of attaching the XML code to the Excel file and makes it a lot easier to make changes to the ribbon.
When you first open the file, you’ll see a blank canvas such as this:
Start with clicking on the Folder icon to load up the Excel file that you want to modify. Any Excel file will do. Next, go to the Insert menu and click on Office 2010 Custom UI Part:
This will create the xml file for Office 2010 and newer versions of the ribbon. If you want to ensure these ribbon modifications also work on Office 2007, then you will want to also insert the file for Office 2007 Custom UI Part. If the file isn’t going to be used on an older version of Excel, this isn’t necessary, but it also doesn’t require much additional effort.
The Custom UI Editor includes some sample code within it that you can automatically load so that you don’t have to start from scratch. With one of the xml files selected, let’s go back to the Insert menu, and this time click on Sample XML and Custom Tab.
This will insert some xml to get you started:
What the code has done is created a tab called Custom Tab and within that, created a Custom Group. Lastly, it has also added a button called Custom Button, which is a large size and uses a HappyFace icon that is built-in within Excel. Here’s what that looks like in the actual Excel file:
The one thing that’s left to do is to link the button to some VBA code so that it does something when you click on it. I’ll cover that towards the end of this post.
If you wanted to run this same xml code for the older version of Excel (2007), then everything would work the same except for the very first line. Instead of this:
To check that your code is correct, you can click on the red checkmark icon at the top:
And if you get this message:
Then your code is good to go and doesn’t contain any (obvious) errors.
Adding a custom button to the Home tab
Using the above example, you can customize the Excel ribbon to create a group and custom buttons inside of a new tab. However, I prefer simply adding any custom buttons on the Home tab to make them easy to find. Unless you have many buttons and macros, you probably don’t need to put them on an entirely separate tab of their own.
If you don’t want to create a new tab and just want to put your buttons in an existing tab, then you can use the following code to put them on the Home tab:
<tab idMso=”TabHome”>
When you use a reference of ‘Mso’ that means it is an existing Microsoft tab/group/image. You need to refer to the correct name (see further down for a list of groups and tabs) and then you can put your custom group or button in that tab rather than creating a new one. Here’s what the full code would look like by changing this one reference from the above example:
In Excel, I still have my custom group, but now it isn’t on its own tab. Instead, it goes to the end of the tab:
If it’s too far to the end what you can do is insert it before a certain group. Let’s say I want to put it just before the Alignment group. Then I just need to adjust the code slightly to add the insertBeforeMso (this is case-sensitive) attribute for the group tag:
And now my custom tab shows up a lot earlier in the home tab:
Another thing I can do is also remove some groups. If I don’t want the font group, I can add the following line of code in the Custom UI Editor:
<group idMso=”GroupFont” visible=”false”/>
Here is the updated code:
And here’s what the ribbon looks like:
You could make all the Microsoft tabs and groups invisible if you wanted to and can control where you custom group goes. The key is knowing the correct names.
Names of the Microsoft tabs and groups
Here is a list of the Microsoft tabs and the reference you will want to use when modifying the ribbon:
Tab Name
RibbonX Referenece
Home
TabHome
Insert
TabInsert
Draw
TabDrawInk
Page Layout
TabPageLayoutExcel
Formulas
TabFormulas
Data
TabData
Review
TabReview
View
TabView
Developer
TabDeveloper
Here are the main groups from the Home tab:
Group Name
RibbonX Reference
Font
GroupFont
Alignment
GroupAlignmentExcel
Number
GroupNumber
Styles
GroupStyles
Cells
GroupCells
Editing
GroupEditingExcel
Clipboard
GroupClipboard
Undo
GroupUndo
There are more groups (from other tabs) but for this purpose, I just included the most common ones.
Adding an image to a button
If you want to use an existing Microsoft image for your button, then you can view the imageMso gallery here. Once you find the image you want to use, just put that in place of the HappyFace image in the earlier code.
However, suppose you want to make a custom image. I’m going to create one using the Amazon logo to create a button that will open my browser to the Amazon.com website.
For starters, I need to get an image. For large ribbon buttons, you want to aim for a size of 32 x 32 and for smaller images, 16 x 16. As long as it’s a square image, however, you should be okay. Wide images will stretch and won’t look as good. This is the image I’m going to use:
I’m going to use just a simple code for creating the button, which looks as follows:
I haven’t associated an image to this button yet. To do that, I’m going to click on the xml file and go back to the Insert menu. This time, I’m going to select Icons. This will open up launch a dialog box where I can now select the image I want to use from my computer. Once I’ve selected it, it now shows up underneath my xml file:
I can right-click on the name ‘amazon’ to change the id to something else. Whatever if it is, that’s what I need to reference in my xml code. Since it’s not a Microsoft image, I just add the following attribute:
image=”amazon”
And here is my full code:
If I open up Excel, this is what my custom button looks like:
But right now, my button doesn’t do anything. That leads us to the last section on how to customize the Excel ribbon: callbacks.
Setting up callback macros
A callback tells the button which code to run. So that means you need some VBA code to begin with, otherwise, the button isn’t going to do anything. I’m going to create a simple macro that will just open the Amazon.com website:
The callback function itself can be generated from the Custom UI Editor. If I click on the icon next to the checkmark that looks like a block of code:
The application will produce the VBA code I need to put into my Excel file:
I’m going to copy and paste that back into VBA. However, I need to add a line in between as that code only sets up the macro, it doesn’t do anything yet. I need to reference the macro I created earlier. The full callback macro looks as follows:
‘Callback for btnAmazon onAction Sub Callback(control As IRibbonControl)
Amazon
End Sub
Now when the button is pressed, the ‘Amazon’ macro will run, which opens the Amazon.com website. You can create a custom button for each macro you want to run and assign an image to each one. All you need to do is to use the callback macro to link the button to the code you want to run.
If you liked this post on How to Customize the Excel Ribbon Using the Custom UI Editor, please give this site a like on Facebook and also be sure to check out some of the many templates that we have available for download. You can also follow us on Twitter and YouTube.
If you want to customize the ribbon in Excel then you know simply changing doing it through the front end is only going to work on your computer. The customization isn’t technically saved within the file and it won’t move from one file to another unless you actually adjust the xml.
It’s by no means an easy process, but if you’re just looking to add a custom tab with some buttons for some macros that you have, I can show you a quick way to do that as painlessly as possible. It’s by no means comprehensive, but it’ll get the job done.
Step 1: Open your Excel file in a program like Winzip/7-Zip
If you right-click on your Excel file you should have an option that says Open Archive if you have a program like Winzip or 7-Zip installed. There, you should see something that looks like this:
These are the files and folders that are within the Excel file itself.
Step 2: Create a folder called customUI
Step 3: Open up notepad and paste the following into it:
Step 6: Close the archive, save changes and open the file
You should now see the two buttons at the end of the home tab:
The problem is that they don’t do anything just yet. In the code that you copied back in Step 3, there were some callback items (‘CB’) that we need to reference back to inside VBA. Those are effectively a link from when a user presses the button on the ribbon to the macro that you’ve coded.
What you’ll need to do now is go into a module within VBA and enter the following code:
Sub cbMacro1(control As IRibbonControl) ***name of your macro*** End Sub
Sub cbMacro2(control As IRibbonControl) ***name of your macro*** End Sub
In the subprocedures for the two buttons all you need to do is call your macro (in place of the code within the ***), and now the buttons should work.
Modifying the code to add more customization
In Step 3, the code there was for a couple of buttons that you could add to your ribbon. If you want to add more, simply following the sequence you can easily add another button:
The button id doesn’t matter too much itself. The label is what will show up underneath the button. ImageMso is the image that will show up. If you do a google search for ImageMso you’ll find what some of the different codes are.
You can shrink the image down to “small” by changing the size attribute here. Note that some of the ImageMso’s are already small and can’t be made large, but you can shrink large ones down in size.
The most important item when adding a button is the onAction attribute as this is what your callback code needs to reference inside VBA. Obviously the more consistently you name your buttons the easier it’ll be to add more without getting lost in your code.
Adding a new tab
In the above code, I added the buttons to the Home tab. However, if you’ve got more that you want to add then you can put them on an entirely new tab instead.
What you can do then is just change the following:
<tab idMso="TabHome">
into this:
<tab id="MyMacros" label="My Macros">
And now all of your macros will be saved onto a new tab rather than take up space on the Home tab. You can also group your buttons based on the group code as well.
This is a small sample of what you can do to customize the ribbon in Excel. The benefits of making these changes through XML and not within Excel’s interface is that your modifications are stored within the file regardless of what computer you open it on and won’t be lost.
There is another way that you can do this using the CustomUI editor and I’ll include that in a later post.
If you liked this post on How to Customize the Ribbon in Excel Using XML, please give this site a like on Facebook and also be sure to check out some of the many templates that we have available for download. You can also follow us on Twitter and YouTube.