How to save custom functions and macros to an Add-In
Table of Contents
1. How to save custom functions and macros to an Add-In
Macros and custom functions are great, they can automate many tedious tasks. To have them available whenever you need them, save user defined functions and macros to a personal add-in.
When you open a new excel spreadsheet, these functions and macros are ready to be used.
How to quickly create an empty Add-in for Excel 2007 and later versions
- Save a blank workbook as a Excel Add-In (*.xlam) in your Add-In folder.
I named it MyAddIn.xlam. - Press with left mouse button on Office button.
- Press with left mouse button on "Excel options" button.
- Press with left mouse button on "Add-Ins" tab.
- Select Excel Add-ins.
- Press with left mouse button on "Go..." button.
- Press with left mouse button on "Browse.." button.
- Select MyAddIn.xlam.
- Press with left mouse button on OK.
- Make sure MyAddIn is enabled in Add-In Manager.
- Press with left mouse button on OK.
Recommended article
Recommended articles
Save links to your favorite macros in a personal tab on the ribbon for easy access and become more productive. […]
Add custom functions to your personal add-in
- Press with left mouse button on "Developer" tab on the ribbon.
- Double press with left mouse button on Myfunctions.xlam in project window.
- Press with left mouse button on "Insert" tab.
- Press with left mouse button on Module.
- Copy and paste custom functions and macros to code window.
How to use custom functions in an Add-In
- Select a cell.
- Type the custom function name in formula bar. Press Enter.
If you don´t know the name of your custom function, continue to step 3. - Press with left mouse button on "Insert Function" button.
- Select category "User Defined".
- Select your custom function.
- Press with left mouse button on OK.
How to use macros in an Add-In
- Press with left mouse button on "Developer" tab.
- Press with left mouse button on Macros button.
- Type the name of your macro.
- Press with left mouse button on "Run" button
2. How to add a custom-made item to the shortcut menu - VBA
This post describes how to add a new custom-built item to the shortcut menu in Excel, when you press with right mouse button on a cell a context menu appears. Let's add a new item to the cell context shortcut menu and link it to a macro.
I demonstrated in a previous post how to automatically select cell A1 on every sheet in a workbook using vba. This is the macro we are going to use. This macro selects cell A1 in all sheets in the active workbook. Copy the following code to a regular code module, see details below.
Macro VBA code:
Sub SelectA1() Dim sht As Worksheet, csheet As Worksheet Application.ScreenUpdating = False Set csheet = ActiveSheet For Each sht In ActiveWorkbook.Worksheets If sht.Visible Then sht.Activate Range("A1").Select ActiveWindow.ScrollRow = 1 ActiveWindow.ScrollColumn = 1 End If Next sht csheet.Activate Application.ScreenUpdating = True End Sub
This vba code adds an item on the cell shortcut menu. Copy the code into the standard code module.
Sub AddItemShortCutMenu() Dim Shortcut As CommandBar Dim NewItem As CommandBarButton Set Shortcut = Application.CommandBars("Cell") Set NewItem = Shortcut.Controls.Add(Type:=msoControlButton) With NewItem .OnAction = "SelectA1" .Caption = "Select cell A1 in all sheets in this workbook" End With End Sub
This vba code removes the item on the cell shortcut menu. Copy the code into the standard code module.
Sub RemoveItemFromShortCutMenu() On Error Resume Next CommandBars("Cell").Controls("Select cell A1 in all sheets in this workbook").Delete End Sub
The code below adds the item on the shortcut menu whenever this workbook is opened. It also deletes the Item from the shortcut menu whenever the workbook is closed. Copy the code below and paste it into the ThisWorkbook code window. You open ThisWorkbook code window by double-press with left mouse button oning ThisWorkbook in the project explorer.
Private Sub Workbook_Open() Call AddItemShortCutMenu End Sub Private Sub Workbook_BeforeClose(Cancel As Boolean) Call RemoveItemFromShortCutMenu End Sub
Create an add-in
Now if you save the workbook as an add-in and install the add-in, the added item on the shortcut menu is always available.
How to create an add-in: Save your custom functions and macros in an Add-In
How to copy vba code into a standard code module
- Press Alt-F11 to open visual basic editor
- Press with left mouse button on Module on the Insert menu
- Copy and paste vba code.
- Exit visual basic editor
Recommended articles
Save links to your favorite macros in a personal tab on the ribbon for easy access and become more productive. […]
Recommended articles
Table of Contents How to save custom functions and macros to an Add-In How to add a custom-made item to […]
Example custom functions and macros
Which macro shortcut keys do you use? (Code for excel and outlook)
Personal.xls (Daily dose of excel)
Recommended blog posts:
Excel Macro Toolbar
Menu for favorite macros in Excel 2007-2010 (for all workbooks)
Making Your Custom Functions Available Anywhere
Add in category
Table of Contents Split data across multiple sheets - VBA Add values to worksheets based on a condition - VBA […]
Merge Ranges is an add-in for Excel that lets you easily merge multiple ranges into one master sheet. The Master […]
This article demonstrates an array formula that searches two tables on two different sheets and returns multiple results. Sheet1 contains […]
Macro category
Table of Contents Excel monthly calendar - VBA Calendar Drop down lists Headers Calculating dates (formula) Conditional formatting Today Dates […]
Table of contents Save invoice data - VBA Invoice template with dependent drop down lists Select and view invoice - […]
This blog post demonstrates how to create, populate and change comboboxes (form control) programmatically. Form controls are not as flexible […]
User defined function category
Table of Contents Search for a file in folder and sub folders - User Defined Function Search for a file […]
This article demonstrates two ways to calculate the number of times each word appears in a given range of cells. […]
This article demonstrates two formulas that extract distinct values from a filtered Excel Table, one formula for Excel 365 subscribers […]
Excel categories
4 Responses to “How to save custom functions and macros to an Add-In”
Leave a Reply
How to comment
How to add a formula to your comment
<code>Insert your formula here.</code>
Convert less than and larger than signs
Use html character entities instead of less than and larger than signs.
< becomes < and > becomes >
How to add VBA code to your comment
[vb 1="vbnet" language=","]
Put your VBA code here.
[/vb]
How to add a picture to your comment:
Upload picture to postimage.org or imgur
Paste image link to your comment.
Contact Oscar
You can contact me through this contact form
spellnumber command
Having lost my macros a number of times using Excel 2010, I have discovered that Personal.xlam is NOT saved on exit even if changes have been made! You must remember to save it manually or lose all your hard work. Does anyone know how to FORCE a save if the file is "dirty"?
[…] can find instructions to do this in many places. A quick search revealed this site which also includes pictures; at step 5 in their “Add custom functions to your personal add-in” […]
I've made very bad experience with your approach and thus ended up with a Workbook with all the macros (I call them services) which includes a test environment for a regression test etc. Whenever I've changed something in this Workbook I 'Setup/Renee' the Addin. This is not just saving it as Addin but is a much more complex process. It has to be considered that the Addin is open and referenced by other open Workbooks. And last but not least these two instances - which only differ by their extension (xlsb versus xlam) have to be considered when the Workbook is opened.