To call a VBA module Sub procedure from an Excel worksheet, you need to assign the macro to a button (form control or ActiveX) or run it via the Macros dialog—no special setup beyond having the macro written and saved in a standard module. First, ensure your Sub is declared as Public (or omit the keyword, as module-level procedures default to Public), placed in a standard module (not ThisWorkbook or a sheet), and the workbook is saved as macro-enabled (.xlsm). Then insert a button on the sheet and link it to the Sub name, or press Alt + F8 to open the Macros dialog and run it directly.
What You'll Need
- Microsoft Excel (2010 or later; steps are consistent across versions)
- A macro-enabled workbook (.xlsm extension)
- A VBA Sub procedure written in a standard module (e.g.,
Module1) - Developer tab enabled in Excel (required for inserting controls)
Step-by-Step
- Create or locate your Sub in a standard module
Open the Visual Basic Editor by pressing Alt + F11. In the Project Explorer window, right-click your VBA project, choose Insert > Module (this creates a new standard module likeModule1). In the code window, type your Sub, for example:Public Sub UpdateReport() Range("A1").Value = "Updated: " & Now() MsgBox "Report updated successfully!" End Sub
Ensure the procedure name matches exactly what you’ll assign to a button later. Avoid spaces or special characters in the name. - Save the workbook as macro-enabled
Go to File > Save As. In the Save as type dropdown, select Excel Macro-Enabled Workbook (*.xlsm). Click Save. If you skip this, your Sub will not be saved with the file, and calling it later will fail. - Enable the Developer tab (if missing)
Click File > Options > Customize Ribbon. In the right-hand list under Main Tabs, check the box for Developer, then click OK. The Developer tab now appears in the ribbon. - Insert a button control on the worksheet
Return to Excel. On the Developer tab, click Insert in the Controls group. Under Form Controls, select the Button (Form Control) icon (the gray rectangle with a border). Drag on your worksheet to draw the button. - Assign the Sub to the button
Release the mouse button—the Assign Macro dialog appears automatically. In the Macro name list, select your Sub (e.g.,UpdateReport). Click OK. If the dialog doesn’t appear, right-click the button and choose Assign Macro…, then select your Sub and click OK. - Test the setup
Click the button on the worksheet. Your Sub should run—e.g., updating cell A1 and showing a message box. If it fails, check for typos in the Sub name or scope (see Troubleshooting).
Common Mistakes
- Sub declared as Private
PlacingPrivate Sub MyMacro()in a standard module makes it unavailable to assign to a button. For worksheet-triggered macros, usePublic Subor omitPublicentirely (default is Public at module level). - Using sheet or workbook event code
Code inThisWorkbookor sheet modules (e.g.,Sheet1) cannot be directly assigned to buttons. Move the Sub to a standard module (e.g.,Module1). - Incorrect file format
Saving as .xlsx removes macros silently. Always choose .xlsm to retain macro functionality. - Mismatched macro name
TypingUpdateReportin code but assigningUpdatereport(different capitalization) won’t work—Excel treats names as case-insensitive, but spaces or punctuation (e.g.,Update Report!) cause errors. - Macro security blocking execution
If Excel disables macros on open (showing a security warning), the button click may do nothing. Enable macros via the Security Warning bar at the top, or set Trust access to the VBA project under File > Options > Trust Center > Trust Center Settings > Macro Settings.
Tips
- Use descriptive names
Prefix Sub names with verbs (e.g.,RefreshData,FormatSheet) and avoid abbreviations to reduce confusion. - Pass worksheet context
To make your Sub more flexible, useTarget As Rangeparameters and call it withCall MySub(ActiveCell), though assigning parameters via buttons requires a wrapper Sub (see FAQ). - Test with the keyboard shortcut
After writing the Sub, press Alt + F8 to verify it runs before assigning to a button. - Hide the Developer tab for users
After building your button, disable the Developer tab via File > Options > Customize Ribbon if you don’t want end users accessing the VBA editor. - Use comments in code
Explain what each Sub does with a comment line (e.g.,' Updates report header with current timestamp)—this helps troubleshooting later.
Troubleshooting
If your button click does nothing or shows an error, follow these checks in order:
- Verify the workbook is saved as .xlsm
Close and reopen the file. If the macro disappears, the file was saved as .xlsx. Re-save as .xlsm and reassign the macro. - Check macro security settings
Go to File > Options > Trust Center > Trust Center Settings > Macro Settings. Select Enable all macros temporarily to test (not recommended for production), or Disable all macros with notification and ensure the security warning bar appears on opening. - Confirm the Sub is Public and in a standard module
In the VBA editor, check the procedure declaration line. It must readPublic Sub Name()orSub Name()inside a module under Modules (not Class Modules or Sheet Objects). - Reassign the macro to the button
Right-click the button, choose Assign Macro…, deselect the current entry, then re-select your Sub from the list. Click OK to refresh the link. - Check for compile errors
In the VBA editor, click Debug > Compile VBA Project. If errors appear (e.g., undefined variables), fix them before testing again. - Try the Macros dialog directly
Press Alt + F8, select your Sub, and click Run. If this works but the button doesn’t, the issue is with the button assignment—not the Sub.
Frequently Asked Questions
Can I pass arguments (like a cell reference) when calling a Sub from a worksheet button?
No—Form Control buttons only run Subs with no parameters. To simulate argument passing, create a wrapper Sub with no arguments that calls your main Sub with the needed values. For example:
Public Sub RunWithCell()
Call UpdateReport(ActiveCell)
End Sub
Assign RunWithCell to the button instead of UpdateReport.
Why does Excel say “Cannot run the macro…” even though the name appears in the list?
This usually means the macro is in a hidden module or the workbook is open in read-only mode. Close and reopen the file in edit mode. Also ensure the Sub isn’t inside a class module (e.g., UserForm) or a worksheet’s code pane.
Do I need to declare variables inside the Sub?
No—variables declared with Dim inside the Sub are local and safe. Just avoid using reserved keywords (e.g., Sub, End) for variable names.
Can I call a Sub from multiple buttons?
Yes—each button can be assigned the same Sub. If you update the Sub later, all buttons using it will reflect the changes automatically.
Is there a way to call a Sub without using a button?
Yes. Press Alt + F8 to open the Macros dialog and run it directly. You can also assign a keyboard shortcut by clicking Options in the Macros dialog and entering a key (e.g., Ctrl + Shift + R).
What happens if I delete the module containing the Sub?
The button will show an error (e.g., “Macro not found”) when clicked. Re-add the module or recreate the Sub to restore functionality. Always back up macros by exporting modules (in VBA editor: File > Export File) before major changes.