Fix Excel VBA Code Not Running When a Button Is Clicked
Question details
The user needs to fix an Excel VBA macro that executes successfully from the Macro dialog but fails to trigger when assigned to a form control or ActiveX button.

- Product
- Microsoft Excel
- Device & OS
- not provided
- Scenario
- Triggering an assigned VBA macro using a custom button inside an Excel workbook.
- Observed behavior
- The macro runs correctly from the dialog box, but clicking the assigned button does nothing or produces an error, possibly due to a recent 32-bit to 64-bit Office upgrade.
Ensure that macros are enabled in your Trust Center settings and verify whether your Microsoft Office installation is 32-bit or 64-bit.
Test the Macro and Button in a New Workbook
Determine if the issue is caused by a corrupted workbook or a malfunctioning form control by isolating the code in a fresh file.
Sometimes, Excel workbooks accumulate corruption over time, causing form controls to disconnect from their assigned macros. Recreating the button and macro in a completely blank workbook helps identify if the issue is file-specific.
Open Excel and create a completely new, blank workbook.
Press ALT + F11 to open the VBA Editor, click Insert > Module, and paste your existing macro code into the new module.
Navigate to the Developer tab on the Excel ribbon, click the Insert dropdown, and select the Button (Form Control) icon. Draw the button on your worksheet.
When the Assign Macro dialog appears, select your pasted macro and click OK. Click anywhere on the sheet to deselect the button, then click the button to see if the macro runs.

Update Windows API Declarations for 64-Bit VBA
If you recently upgraded to 64-bit Microsoft 365, older 32-bit API calls will cause macros to silently fail or error out when triggered from a button.
Try WPS Office for Seamless Macro Compatibility
If Microsoft 365 updates are causing your VBA macros and buttons to break, consider switching to WPS Office. It provides a stable, lightweight environment with excellent support for VBA macros and Excel file formats.

Frequently Asked Questions
Why does my Excel macro run manually but not from a button?
This typically occurs due to a corrupted form control, strict macro security settings blocking active UI content, or incompatible 32-bit API declarations failing silently when triggered via the interface in a 64-bit Excel environment.
How do I correctly assign a macro to a button in Excel?
Right-click the form control button, select 'Assign Macro' from the context menu, choose the desired macro from the list, and click OK. Ensure the macro is stored in a standard module and is not declared as a 'Private Sub'.
What does the PtrSafe attribute do in VBA?
The PtrSafe attribute tells the VBA compiler that the Declare statement is explicitly safe to run in 64-bit versions of Office. It is absolutely required when calling Windows API functions in 64-bit Excel environments.




