Got any Excel/VBA Questions? Free Excel Help.
Creating Excel Add-ins
I am often asked by users 'what is the best way to distribute their macros?' My answer, is without doubt via an Excel Add-in. After all, this is what Add-ins are for. For those that are not sure what an Excel add-in is, it's is nothing more than an Excel Workbook that has been saved as an Add-in, File>Save as \ Microsoft Excel Add-in (*.xla). Once saved and re-opened the Workbook will be hidden and can only be seen in the "Project Explorer" via the Visual Basic Editor. It is NOT hidden in the same way as the Personal.xls as this can be seen (and made visible) via Windows>Unhide.
Once completed users can easily install your Add-in like below:
Most code can be saved to an Excel Add-in without too many changes. Some things to be aware of are:
Ok, once you have created your Add-in you will need to make the macros within it easy for the user to run. This is best achieved by making full use of both the Workbook_AddinInstall and the Workbook_AddinUnInstall Events in the Private Module of the This Workbook Object. Simply double click on This Workbook for the *.xla file and Excel will take you into the Private Module where the code is placed.
Let's look at a simple example of this.
Option Explicit Dim cControl As CommandBarButton Private Sub Workbook_AddinInstall() On Error Resume Next 'Just in case 'Delete any existing menu item that may have been left. Application.CommandBars("Worksheet Menu Bar").Controls("Super Code").Delete 'Add the new menu item and Set a CommandBarButton Variable to it Set cControl = Application.CommandBars("Worksheet Menu Bar").Controls.Add 'Work with the Variable With cControl .Caption = "Super Code" .Style = msoButtonCaption .OnAction = "MyGreatMacro" 'Macro stored in a Standard Module End With On Error GoTo 0
End Sub
Private Sub Workbook_AddinUninstall() On Error Resume Next 'In case it has already gone. Application.CommandBars("Worksheet Menu Bar").Controls("Super Code").Delete On Error GoTo 0 End Sub
This code will be all you need to add a single menu item (called Super Code) to the end of the existing Worksheet Menu Bar as soon as the Add-in is installed by the user via Tools>Add-ins. When the Super Codemenu item is clicked a macro (that is within a standard module of the add-in) is run. As mentioned earlier, the above code MUST be placed in the Private Module of ThisWorkbook for the Add-in.
If you want the Super Code menu item added, say before the Format menu item, you could use some code like this.
Option Explicit Dim cControl As CommandBarButton Private Sub Workbook_AddinInstall() Dim iContIndex As Integer On Error Resume Next 'Just in case 'Delete any existing menu item that may have been left Application.CommandBars("Worksheet Menu Bar").Controls("SuperCode").Delete 'Pass the Index of the "Format" menu item number to a Variable. 'Use the FindControl Method to find it's Index number. ID number _ is used in case of Customization iContIndex = Application.CommandBars.FindControl(ID:=30006).Index 'Add the new menu item and Set a CommandBarButton Variable to it. 'Use the number passed to our Integer Variable to position it. Set cControl = Application.CommandBars("Worksheet Menu Bar").Controls.Add(Before:=iContIndex) 'Work with the Variable With cControl .Caption = "Super Code" .Style = msoButtonCaption .OnAction = "MyGreatMacro" 'Macro stored in a Standard Module End With On Error GoTo 0 End Sub
There would be no need to change the Workbook_AddinUninstall() code in this case.
The above examples actually have the all the menu item code in the Workbook_AddinInstall and Workbook_AddinUnInstall Not a problem when the code is only adding one menu item. If however, you will be adding more then one and perhaps even Sub menus, you should place it in a Procedure (or 2) inside a standard Module. Then use some code as shown below:
Private Sub Workbook_AddinInstall() Run "AddMenus"
End Sub Private Sub Workbook_AddinUninstall() Run "DeleteMenu" End Sub
Then in the standard module put some code perhaps like this
Sub AddMenus() Dim cMenu1 As CommandBarControl Dim cbMainMenuBar As CommandBar Dim iHelpMenu As Integer Dim cbcCutomMenu As CommandBarControl '(1)Delete any existing one.We must use On Error Resume next _ in case it does not exist. On Error Resume Next Application.CommandBars("Worksheet Menu Bar").Controls("&NewMenu").Delete '(2)Set a CommandBar variable to Worksheet menu bar Set cbMainMenuBar = Application.CommandBars("Worksheet Menu Bar") '(3)Return the Index number of the Help menu. We can then use _ this to place a custom menu before. iHelpMenu = cbMainMenuBar.Controls("Help").Index '(4)Add a Control to the "Worksheet Menu Bar" before Help 'Set a CommandBarControl variable to it Set cbcCutomMenu = cbMainMenuBar.Controls.Add(Type:=msoControlPopup, Before:=iHelpMenu) '(5)Give the control a caption cbcCutomMenu.Caption = "&New Menu" '(6)Working with our new Control, add a sub control and _ give it a Caption and tell it which macro to run (OnAction). With cbcCutomMenu.Controls.Add(Type:=msoControlButton) .Caption = "Menu 1" .OnAction = "MyMacro1" End With '(6a)Add another sub control give it a Caption _ and tell it which macro to run (OnAction) With cbcCutomMenu.Controls.Add(Type:=msoControlButton) .Caption = "Menu 2" .OnAction = "MyMacro2" End With
'Repeat step "6a" for each menu item you want to add. 'Add another menu that will lead off to another menu 'Set a CommandBarControl variable to it Set cbcCutomMenu = cbcCutomMenu.Controls.Add(Type:=msoControlPopup) ' Give the control a caption cbcCutomMenu.Caption = "Next Menu" 'Add a control to the sub menu, just created above With cbcCutomMenu.Controls.Add(Type:=msoControlButton) .Caption = "&Charts" .FaceId = 420
.OnAction = "MyMacro2"
End With
On Error GoTo 0
End Sub Sub DeleteMenu() On Error Resume Next
Application.CommandBars("Worksheet Menu Bar").Controls("&NewMenu").Delete On Error GoTo 0
End Sub
See also:
Free Training Course: Lesson 1 - Excel Fundamentals
See also: Index to Excel VBA Code; Index to Excel Freebies; Lesson 1 - Excel Fundamentals; Index to how to… providing a range of solutions
Click here to visit our Free 24/7 Excel/VBA Help Forum where there are thousands of posts you can get information from, or you can join the Forum and post your own questions.