Return to Top Page
Note:
- Files containing VBA (macros)
must be saved with the .xlsm extension.
- Save the add-in as .xlam.
Preparing for VBA in Excel
Display the "Developer" Tab
- Open Excel, create a new workbook or open an existing one.
- Ribbon's "File" Tab =>
Select "Options" Menu.
- When the "Excel Options" dialog box appears, click "Customize Ribbon."
- In the "Customize Ribbon" section on the right, check the "Developer" box.
- Click the "OK" button to close the dialog box.
Ensure the "Developer" tab is displayed on the ribbon.
Creating Code in the VBA Editor
- "Developer" tab => Click "Visual Basic" button.
- The VBA editor opens, select "Module" from the "Insert" menu.
- Enter the following code into the newly created module.
Sub HelloWorld()
MsgBox "Hello, World!"
End Sub
- Close the VBA Editor and return to Excel.
- Click the "Macros" under the "Developer" tab
(or press Alt + F8)
to open the "Macro" dialog box.
Select "HelloWorld" and click the "Run" button to execute
Add a Button in Excel to Run VBA
Note:
Macros need to be enabled in the settings. Please be aware of the security risks.
- Open "File" => "Options"
- "Security Center" =>
Click "Security Center Settings"
- Macro Settings
Change to "Enable All Macros"
Check "Trust access to the VBA project object model"
- Restart Excel
Add Form Control Button
- "Development" Tab => "Design Mode"
Press to enter Design Mode
- Click the "Insert" button
- "Select 'Button' under 'Form Control'"
Place Buttons by Dragging on the Sheet
- The "Record Macro" dialog box appears,
Create New (or select existing macro)
Enter VBA Code
- The VBA editor will open, so enter the following code
Sub RunMacro()
MsgBox "Button clicked!"
End Sub
- Save using the Save button or Ctrl+S
- Close the VBA Editor
- Close the dialog by pressing the OK button
- Press the Design Mode button to exit Design Mode
Execution
- Click the button
Changes to Registration Macro
- Right-click on the button
- Select "Record Macro"
How to Use External File VBA
Can be achieved using the following methods.
(i) Import External Files
Note: Enable "Trust access to the VBA project object model"
- Sub ImportModule()
Dim vbProj As Object
Set vbProj = ThisWorkbook.VBProject
vbProj.VBComponents.Import "\path\to\module.bas"
End Sub
(ii) Execute External File VBA
- Sub RunMacroFromAnotherWorkbook()
Dim wb As Workbook
Set wb = Workbooks.Open("\path\to\otherWorkbook.xlsm")Application.Run "otherWorkbook.xlsm!MacroName"
Close without saving
End Sub
(iii) Use Add-ins
- Save Other Files as Add-in (.xlam)
- Enable Add-ins in Excel
- Run the macro with Application.Run "AddInName.xlam!MacroName"
(iv) Library Configuration (Reference Settings)
- Open "Tools" → "References" in the VBA Editor (VBE)
- Add Other Excel Files as Library
Can directly call included VBA functions.
- Sub RunVBAFromLibrary()
Dim obj As New MyLibrary.Module1
Call obj.MySub
End Sub
(v) Execute External Scripts (VBS)
(Deprecated as of 2024)
- Save the following to test.vbs
Dim objExcel, objWorkbook
Set objExcel = CreateObject("Excel.Application")
Set objWorkbook = objExcel.Workbooks.Open("C:\path\to\otherWorkbook.xlsm")objExcel.Application.Run "otherWorkbook.xlsm!MacroName"
objWorkbook.Close False
objExcel.Quit
- Execute
> cscript test.vbs
(vi) Execute with PowerShell
- Save the following as test.ps1
$excel = New-Object -ComObject Excel.Application
$excel.Visible = $false # Hide Excel
$workbook = $excel.Workbooks.Open("C:\Users\YourName\Documents\test.xlsm")
$excel.Run("MyMacro") # 実行するマクロ名
$workbook.Close($false) # Close without saving
$excel.Quit()
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($excel)
- Execution
> powershell -ExecutionPolicy Bypass -File "test.ps1"