1

I have a Excel based application - written in VBA. I would like to create a custom UI for the application during runtime.

e.g., when the user opens my excel, a VBA method should run to add a new tab, groups and controls to the Excel UI. And upon closing the excel, that custom UI should be removed.

The excel file itself is autogenerated from csv and txt files - therefore I can't embed a CustomUI.xml file inside the XLS (zip) file as few StackOverflow posts suggested.

I came across VBA code that can manipulate the ribbon during runtime.... Write Excel Addin with VBA and then Put a button that trigger it

and tried the following to create my custom UI under Add-Ins tab

Sub CreateMyMenu()
    Dim myCB As CommandBar
    Dim myCBtn1 As CommandBarButton
    Dim myCBtn2 As CommandBarButton
    Dim myCPup1 As CommandBarPopup
    Dim myCPup2 As CommandBarPopup
    Dim myCP1Btn1 As CommandBarButton
    Dim myCP1Btn2 As CommandBarButton

    ' Delete the CommandBar if it exists already
    On Error Resume Next
    Application.CommandBars("MyUI").Delete

    ' Create a new CommandBar
    Set myCB = CommandBars.Add(name:="MyUI", Position:=msoBarFloating)


    ' Add button 1 to this bar
    Set myCBtn1 = myCB.Controls.Add(Type:=msoControlButton)
    With myCBtn1
     .Caption = "My Tool"
     .Style = msoButtonCaption   '<- force caption text to show on your button
    End With

    ' Show the command bar
    myCB.Visible = True
End Sub

When I run this sub, the UI is created like this...

enter image description here

Is there a way to:

  1. Control the name of the group other than "Custom Toolbars"?
  2. Control the UI position (not under Add-Ins tab but on a newly created Tab)?
  3. How can I remove the "Menu Commands" group and the button? (did I do it somehow?)
Pawel Czyz
  • 1,651
  • 4
  • 17
  • 21
NirMH
  • 4,769
  • 3
  • 44
  • 69
  • Using your code above, I don't get "Menu Commands" in my AddIn tab, so you've probably add it somehow. – FAB Jun 02 '19 at 14:15

0 Answers0