https://github.com/BriargateExcel/AddingVBARibbonElements
VBA code to add Custom Toolbars to the Excel Ribbon
https://github.com/BriargateExcel/AddingVBARibbonElements
Last synced: 4 months ago
JSON representation
VBA code to add Custom Toolbars to the Excel Ribbon
- Host: GitHub
- URL: https://github.com/BriargateExcel/AddingVBARibbonElements
- Owner: BriargateExcel
- Created: 2020-06-28T16:18:39.000Z (almost 5 years ago)
- Default Branch: master
- Last Pushed: 2020-07-19T14:48:25.000Z (over 4 years ago)
- Last Synced: 2024-08-13T07:18:14.139Z (8 months ago)
- Language: VBA
- Size: 272 KB
- Stars: 2
- Watchers: 2
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
- jimsghstars - BriargateExcel/AddingVBARibbonElements - VBA code to add Custom Toolbars to the Excel Ribbon (VBA)
README
**Adding Your Own Ribbon Elements to Excel**
Here is some code to put in your VBA project to add your own commands to the Excel Ribbon.
These new commands appear in the Ribbon as “Add-ins” in a section called “Custom Toolbars.”


**Instructions for using:**
1. The code below can go in any VBA Code Module or as a standalone module
2. Give your command bar a name by specifying `CommandBarName`. Spaces are allowed.
3. Specify the routine to execute in `NewButton.OnAction`
4. Specify the caption that appears when the user hovers over the button in `NewButton.Caption`. Spaces are allowed.
5. Specify your button’s appearance in `NewButton.FaceID`. There is an extensive collection of images in https://bettersolutions.com/vba/ribbon/face-ids-2003.htm**How it works:**
1. Auto_Open executes when you launch your workbook
2. Auto_Open then
1. Deletes `CommandBarName` if it already exists
2. Creates `CommandBarName`
3. Builds the button(s)
4. Makes `CommandBarName` visible
3. When the user hovers over a button, they see “`CommandBarName: ButtonCaption`”
4. `Auto_Close` executes when you close your workbook to delete the command bar**How I use it:**
- I have a set of buttons in my Personal.xlsb that are available in all my workbooks
- I have unique buttons for each workbook that allow me to execute routines quickly and easily```
Option ExplicitPrivate Const CommandBarName As String = "Table Builder"
Private Type CodeType
CustomBar As CommandBar
End TypePrivate This As CodeType
Public Sub Auto_Open()
' https://bettersolutions.com/vba/ribbon/face-ids-2003.htm for FaceIDs
On Error Resume Next
CommandBars(CommandBarName).Delete
On Error GoTo 0Set This.CustomBar = CommandBars.Add(Name:=CommandBarName)
BuildButton "RourineToExecute", "Button Caption", 81 ' Capital B
This.CustomBar.Visible = TrueEnd Sub ' Auto_Open
Private Sub BuildButton( _
ByVal RoutineToExecute As String, _
ByVal Caption As String, _
ByVal FaceID As Long)' Build one button on the command bar
Dim NewButton As CommandBarButton
Set NewButton = This.CustomBar.Controls.Add(msoControlButton)
NewButton.OnAction = RoutineToExecute
NewButton.Caption = Caption
NewButton.FaceID = FaceID
End Sub ' BuildButtonPublic Sub Auto_Close()
On Error Resume Next
CommandBars(CommandBarName).Delete
On Error GoTo 0End Sub ' Auto_Close
```