Since office 2007 and higher versions are based on XML hence, its UI can be manipulated with the help of XML and user can bring his own features as desired. In this post, I will guide you through creating your own ribbon with various controls that you can use to accomplish various tasks in Excel.
Step 1: Open a blank workbook and save it a macro enabled format as shown below (I named it as “CustomAddin.xlsm“):
Step 2: Close the file and open Microsoft Custom UI Editor (you can download it from here). Its a free tool. And browse “CustomAddin.xlsxm” file using Open menu as shown below:
Step 3: Once file is open it will look like as below:
Step 4: Next you need to insert your own custom xml to cusomize the UI, hence, click on Insert Menu, Under Insert Menu you will see Sample XML, click “Excel – A Custom Tab” (since we intended to add a new tab) as shown below:
Now a Custom XML is available which we can manipulate to bring our own buttons on the ribbon. Followings are required to be changed:
CustomUI xml Tags
- <ribbon> : referred to a physical ribbon in the workbook
- <tabs> : a collection of tabs
- <tab> : an individual tab that you want to customize as your own, each tab will have a unique id. Label attribute sets to display tab text in Excel.
- <group> : one tab can have multiple groups, each group will have a unique id. Label attribute sets to display group text in Excel.
- <button> : actual controls which will be placed under respective group, each button will have following properties:
- id: a unique identifier
- label: the text to be displayed on the button
- size: takes two values “small” or “large”
- onAction: defines an event which will be called, upon clicking the button
- image: an Image which will be displayed on the button
In my example I have customized the ribbon with two buttons Input and Output. Image can be inserted with the help of Insert Icon using Insert menu as shown below.
Step 5: Validate XML, to be very sure the change you made in the xml are valid or not, click on the Validate button as shown below, if you have made valid changes then it will prompt “Custom UI XML is well formatted“:
Step 6: Close Custom UI Editor and open Excel file back in Excel and it will look like as below:
Step 7: Adding Callback, if you click on Input/Output button will do nothing as they are just buttons with no action as of now. Next we need to make them functional hence we need to add callback. Now close the file and open same file back in Custom UI Editor and click on Generate Callback button as shown below (Please note pointed arrow) as shown below:
If you notice, Custom UI Editor automatically determines the number of actions from xml and generates those many callbacks. Callbacks are empty functions which we need to use to write program under. For now you have to copy these callback functions and keep somewhere for next step.
Step 8: Close Custom UI Editor and open “CusotmAddin.xlsm” in Excel and open VBA Editor by pressing ALT + F11 key or click on developer tab and click on Visual Basic button. Now Insert a new module from Insert Menu>>Standard Module and paste copied callback functions as shown below:
Finally you can write whatever you wish under these callbacks and save. Now click on the respective button and you see the code is getting executed by buttons callback as shown below:
Output of Input Button
Output of Output Button
Hope you like the article. Please add you valuable comments and do subscribe for future posts.