How to add a custom Ribbon tab using VBA?

后端 未结 7 645
深忆病人
深忆病人 2020-11-22 14:47

I am looking for a way to add a custom tab in the Excel ribbon which would carry a few buttons. I chanced on some resources addressing it via Google but all look dodgy and o

相关标签:
7条回答
  • 2020-11-22 15:44

    AFAIK you cannot use VBA Excel to create custom tab in the Excel ribbon. You can however hide/make visible a ribbon component using VBA. Additionally, the link that you mentioned above is for MS Project and not MS Excel.

    I create tabs for my Excel Applications/Add-Ins using this free utility called Custom UI Editor.


    Edit: To accommodate new request by OP

    Tutorial

    Here is a short tutorial as promised:

    1. After you have installed the Custom UI Editor (CUIE), open it and then click on File | Open and select the relevant Excel File. Please ensure that the Excel File is closed before you open it via CUIE. I am using a brand new worksheet as an example.

      enter image description here

    2. Right click as shown in the image below and click on "Office 2007 Custom UI Part". It will insert the "customUI.xml"

      enter image description here

    3. Next Click on menu Insert | Sample XML | Custom Tab. You will notice that the basic code is automatically generated. Now you are all set to edit it as per your requirements.

      enter image description here

    4. Let's inspect the code

      enter image description here

      label="Custom Tab": Replace "Custom Tab" with the name which you want to give your tab. For the time being let's call it "Jerome".

      The below part adds a custom button.

      <button id="customButton" label="Custom Button" imageMso="HappyFace" size="large" onAction="Callback" />
      

      imageMso: This is the image that will display on the button. "HappyFace" is what you will see at the moment. You can download more image ID's here.

      onAction="Callback": "Callback" is the name of the procedure which runs when you click on the button.

    Demo

    With that, let's create 2 buttons and call them "JG Button 1" and "JG Button 2". Let's keep happy face as the image of the first one and let's keep the "Sun" for the second. The amended code now looks like this:

    <customUI xmlns="http://schemas.microsoft.com/office/2006/01/customui">
    <ribbon startFromScratch="false">
    <tabs>
    <tab id="MyCustomTab" label="Jerome" insertAfterMso="TabView">
    <group id="customGroup1" label="First Tab">
    <button id="customButton1" label="JG Button 1" imageMso="HappyFace" size="large" onAction="Callback1" />
    <button id="customButton2" label="JG Button 2" imageMso="PictureBrightnessGallery" size="large" onAction="Callback2" />
    </group>
    </tab>
    </tabs>
    </ribbon>
    </customUI>
    

    Delete all the code which was generated in CUIE and then paste the above code in lieu of that. Save and close CUIE. Now when you open the Excel File it will look like this:

    enter image description here

    Now the code part. Open VBA Editor, insert a module, and paste this code:

    Public Sub Callback1(control As IRibbonControl)
    
        MsgBox "You pressed Happy Face"
    
    End Sub
    
    Public Sub Callback2(control As IRibbonControl)
    
        MsgBox "You pressed the Sun"
    
    End Sub
    

    Save the Excel file as a macro enabled file. Now when you click on the Smiley or the Sun you will see the relevant message box:

    enter image description here

    Hope this helps!

    0 讨论(0)
提交回复
热议问题