Macros help to automate repeated tasks in Excel very easily. Sometimes the macros are so effective that you want to reuse them for your future work, use them in other files, or share them. That’s where saving macros in Excel permanently comes in handy.
Excel does not save the macros permanently or across all workbooks by default. That is why in this article, we will show you four practical methods to save macros permanently in Excel. The methods include creating a Personal Macro Workbook, Add-Ins, saving a Macro-Enabled Workbook, and exporting as a .bas file.
➤ You can save macros in Excel permanently to use them later in other files.
➤ Personal Macro Workbook: It saves your macros permanently and makes them available to all Excel workbooks.
➤ Excel Add-Ins: It lets you save the macros permanently and easily share them with others.
➤ Macro-Enabled Workbook: It keeps the macro permanently, but within that specific file.
➤ Exporting as a .bas file: It also stores macros permanently. However, you need to import it into other files when needed.
What Does It Mean to Save Macros Permanently in Excel?
When you want to save macros permanently in Excel, you are making sure the macros are available even after you close the workbook. Plus, you can use it later in that same file or other files.
Below we have an employee attendance dataset. We have created a sample macro to highlight the three lowest values in the Days Absent column. Now we are going to save this VBA macro permanently in Excel using four different methods.
VBA macro
Sub HighlightLowestAbsentDays()
Dim ws As Worksheet
Set ws = ActiveSheet
Dim dataRange As Range
Dim cell As Range
Dim values() As Double
Dim uniqueVals As Collection
Dim val As Variant, i As Long
' Define the range for Days Absent (Column D, row 2 to last used)
Dim lastRow As Long
lastRow = ws.Cells(ws.Rows.Count, "D").End(xlUp).Row
Set dataRange = ws.Range("D2:D" & lastRow)
' Store unique values
Set uniqueVals = New Collection
On Error Resume Next
For Each cell In dataRange
If IsNumeric(cell.Value) Then
uniqueVals.Add cell.Value, CStr(cell.Value)
End If
Next cell
On Error GoTo 0
' Transfer to array and sort ascending
ReDim values(1 To uniqueVals.Count)
For i = 1 To uniqueVals.Count
values(i) = uniqueVals(i)
Next i
Call QuickSort(values, LBound(values), UBound(values))
' Highlight cells matching the 3 lowest values
For Each cell In dataRange
If cell.Value = values(1) Or cell.Value = values(2) Or cell.Value = values(3) Then
cell.Interior.Color = RGB(198, 239, 206) ' light green
Else
cell.Interior.ColorIndex = xlNone ' reset others
End If
Next cell
End Sub
' Sorting function
Sub QuickSort(arr() As Double, ByVal first As Long, ByVal last As Long)
Dim i As Long, j As Long, pivot As Double, temp As Double
i = first: j = last
pivot = arr((first + last) \ 2)
Do While i <= j
Do While arr(i) < pivot: i = i + 1: Loop
Do While arr(j) > pivot: j = j - 1: Loop
If i <= j Then
temp = arr(i)
arr(i) = arr(j)
arr(j) = temp
i = i + 1
j = j - 1
End If
Loop
If first < j Then QuickSort arr, first, j
If i < last Then QuickSort arr, i, last
End Sub
Creating Personal Macro Workbook (Personal.xlsb) in Excel
The Personal Macro Workbook (Personal.xlsb) is the most effective way to save macros in Excel permanently. It is a hidden Excel workbook that opens in the background every time you open Excel.
When you save your macros in this workbook, the macros become available in all workbooks of Excel, not just in the file where you have created them.
Steps:
➤ Go to the Developer tab >> Record Marco.
➤ In the Record Macro dialog box, enter a name for your Macro. We’re creating the workbook with the default Macro name ‘Macro 1’.
➤ Choose Personal Macro Workbook in the ‘Store macro in’ dropdown >> Click OK.
➤ Again, go to Developer >> Stop Recording
➤ Excel will have created your Personal Macro workbook.
➤ Go to Developer >> Visual Basic.
➤ You will find your Personal Macro workbook or PERSONAL.xlsb in the Project Explorer pane on the left-hand side.
➤ You can add more macros to the Personal Macro Workbook following the same recording process.
Save Macros Permanently As Excel Add-In (.xlam)
Excel Add-In is actually a .xlam file. It saves the macros of Excel workbooks. After you create it, the Add-in loads automatically every time you open Excel. It makes its macros available across all your workbooks.
The difference is that the Personal Macro Workbook is stored locally and hidden, but Add-ins are easy to share. So, if you have to save the macros permanently and also need to distribute and maintain them to a group of users, an Add-in is a better method.
Steps:
➤ Press Alt + F11 to open the Visual Basic window
➤ Select Insert >> Module >> Paste your VBA macro.
➤ Click on the Save icon or press Ctrl + S
➤ In the Save as type dropdown, choose Excel Add-In.
➤ Name your file. We have named it ‘Highlight 3 lowest value’. Click Save.
➤ Now you need to install the add-in after saving it, as Excel doesn’t load it automatically. It loads the add-in every time you open Excel.
➤ Go to Developer >> Excel Add-Ins >> Browse
➤ Select the saved .xlam file >> click OK.
➤ The custom Highlight 3 Lowest Value is loaded and enabled. Click OK.
➤ You can also see the .xlam file in the Project Explorer of the Visual Basic for Applications (VBA) editor.
Save Macro as Macro-Enabled Workbook (.xlsm)
A Macro-Enabled Workbook allows you to store VBA macros inside the Excel file. The method does not make the macro available in all workbooks of Excel.
However, it saves the macro permanently within that specific file. If you need to save your macros permanently but don’t need them to be available in all workbooks, you can use this method.
Steps:
➤ Go to File >> Save As or press Ctrl + s
➤ Name your file in the Save As dialog box.
➤ Choose Excel Macro-Enabled Workbook in the ‘Save as type’ dropdown >> Save.
Export or Import as BAS Module Files
A BAS file stores your macro code outside of Excel. That means you can reuse it later or share it with others when you export your macro as a .bas module.
This method also does not make the saved macros available in all Excel workbooks. However, you can save your macros permanently and import them it any Excel file you need.
Steps:
➤ Open the VBA editor.
➤ Click the Module that contains your macro in the Project Explorer. For instance, ours is Module 1.
➤ Right-click the module >> choose Export File…
➤ Save your file with your desired name in the desired location.
➤ Open the workbook where you want to use the macro
➤ Open the VBA editor >> Choose Import File…
➤ Select the BAS file you exported >> click Open
➤ The module will appear under Modules in the Project Explorer. In our case, it is Module 11.
Frequently Asked Questions
How to enable macros permanently in Excel?
➤ Open your Excel file.
➤ Go to File >> Options >> Trust Center.
➤ Select Trust Center Settings >> Macro Settings>> Enable all macros >> click OK.
How to run saved macros in Excel?
➤ Open the workbook where you want to use the saved macros.
➤ Go to Developer >> Macros
➤ Select the macro that you want to run in the Macro name box.
➤ Press the Run button,
How to run macros continuously in Excel?
➤ Go to Developer >> Visual Basic
➤ Double click ThisWorkbook in the Project Explorer.
➤ In the opened workbook, paste the following code:
Private Sub Workbook_Open()
' Call your macro here
Call YourMacroName
End Sub
➤ Save the file as a Macro-Enabled Workbook.
Wrapping Up
In this quick tutorial, we have learnt how to save macros in Excel permanently using four different saving methods. Feel free to download the practice Excel worksheet file and check out how the saving methods for macros actually work out.