First thing's first: Write your code!
Get your function up and running and doing whatever it is you want it to do, and make sure it's doing it properly. There's no point in having an add-in with menu buttons without an operation to perform or if the code is working incorrectly! Get all these details ironed out first, then move on to the next step.
Set up your code for the long haul.
To work with the menus, you'll need to keep a consistent naming convention, otherwise, menus may get lost or broken. There is some error catching that can help with this, but it is not always enough. To be more proactive against these errors, you should keep some variables saved as Constant values. These values are set once in your code and they cannot be changed by the code, that way your menus should always remain functional. For example, these are the values you’ll want for your add-in:
Const conXLMenuBar As String = "Worksheet Menu Bar"The String literal in this first line is a standard name in Excel and should always be this. The
Const conYourAddInButton As String = "Click this button to run your code"Both the variable name (
Clean up after yourself!
To actually get some buttons and make sure your menu bar doesn't explode as a result, there are two main functions you'll need to add to your code modules:
Sub Auto_Close()The program or code may fail from time to time, so we use the
On Error Resume Next
Application.CommandBars(conXLMenuBar).Controls(conYourAddInButton).Delete
Err.Clear
On Error GoTo 0
End Sub
When it doesn't fail, this will look in the standard Excel menu (remember
Time to make the
That’s all there is to the clean up, so let’s move to the open functionality, which actually creates the buttons we want to use.
Sub Auto_Open()At the beginning, you'll see we've actually duplicated the code from the
On Error Resume Next
Application.CommandBars(conXLMenuBar).Controls(conYourAddInButton).Delete
On Error GoTo 0
If Application.Version < 14 Then
MsgBox "Microsoft Office 2010 (or higher) required to use these tools.", _
vbCritical, "Incompatible Verison"
ThisWorkbook.Close False
End If
With Application.CommandBars(conXLMenuBar).Controls.Add(Type:=msoControlButton)
.BeginGroup = True
.Caption = conYourAddInButton
.FaceId = 524
.Style = msoButtonIconAndCaption
.OnAction = "YourFunctionNameGoesHere"
End With
End Sub
The next section does a version check of the Excel program. This is not explicitly required, but if you are working with pivots in 2010, or other objects types/functionality that is not backwards compatible, I highly recommend it. Trying to run these kinds of functions in previous versions requires additional and more in-depth programming to prevent errors that will occur. While taking care of that cleanly is probably better, this catch does help prevent some possible, unexpected errors. If Excel fails the version check, there is a quick warning
The last step is the meat of this whole process: actually creating the button. There are couple options you can use to customize this, but I'll just start with this one. First, we use a
.BeginGroup - Helps organize your buttons if you have more than one
.Caption - Text that will display on the button
- see the
conYourAddInButton reference above
- see the
- Name by which you reference the button later
- e.g. when deleting it from the menu
- Text that will display on the button
.FaceId - Tells the application which built-in icon to use
- There are a few thousand of these, so don't bother trying to memorize them. You can find multiple guides/references online to help you pick a good one.
.Style - Tells Excel how you want to display the button.
- In this case, we are using
msoButtonIconAndCaption , which means we want to show both the icon (FaceId ) and text on the button.
- In this case, we are using
- Tells Excel how you want to display the button.
.OnAction - The most critical piece, as it actually tells Excel what to do when the button is pressed.
- The value you set is a
String within double quotes (“” ) that is the name of the function you made in your other module.
Excel will look through your code for a matching function name and run that, so make sure your function is specified as
Let's wrap it up.
Once that's done, save your Excel file as an add-in. In 2003, this is simply the