VBA macros can run in Excel files stored in SharePoint, but only if the file is opened in the desktop version of Excel and macro security settings allow it. SharePoint itself does not execute VBA code—Excel Online (the browser version) blocks all macros entirely. You must download or open the file in Excel for Windows or Excel for Mac to run or edit VBA. This guide shows how to safely set up, use, and troubleshoot VBA in Excel files synced or stored in SharePoint.
What You'll Need
- A SharePoint site with a document library containing an Excel workbook
- Microsoft Excel installed on your computer (Excel for Microsoft 365, Excel 2021, or Excel 2019; Excel Online won’t work for VBA)
- Appropriate permissions to edit the file in SharePoint (at least Contribute level)
- Macro security settings configured to allow macros (set to "Disable all macros with notification" or lower)
- The Excel file must be saved in a macro-enabled format (.xlsm), not .xlsx
Step-by-Step
- Prepare your Excel file with VBA locally first. Open Excel on your computer, press Alt+F11 to open the Visual Basic for Applications editor. Insert a new module (Insert > Module), paste your VBA code, and save the file as an Excel Macro-Enabled Workbook (.xlsm). Test the macro locally to confirm it works before uploading.
- Upload the .xlsm file to SharePoint. Go to your SharePoint site, navigate to the document library where you want to store the file, and click Upload > Files. Select your .xlsm file and upload it. Wait for the upload to complete and confirm the file appears in the library.
- Open the file in desktop Excel (not Excel Online). In SharePoint, hover over the .xlsm file, click the ellipsis (...), and choose Open > Edit in Excel. If prompted, select "Open in Excel" (not "View in Browser"). This launches Excel for Windows or Mac and opens the file directly from SharePoint (via OneDrive sync if set up).
- Enable macros when prompted. When Excel opens the file, a security warning bar appears at the top: "Macros have been disabled." Click "Enable Content" to allow VBA code to run. If you don’t see this bar, check macro security settings (File > Options > Trust Center > Trust Center Settings > Macro Settings) and ensure it’s not set to "Disable all macros without notification."
- Run or edit the macro. To run the macro, press Alt+F8, select your macro name from the list, and click Run. To edit the code, press Alt+F11 again, locate the module in the Project Explorer, and make changes. Save the file using Ctrl+S—this updates the version in SharePoint automatically if the file is synced, or prompts you to save changes back to SharePoint.
- Ensure version control and permissions are correct. After saving, return to SharePoint and refresh the page. Check that a new version appears in the version history (File > Version History in Excel or the library’s version column). Verify users have appropriate permissions: Contribute or Edit permissions are needed to overwrite the file with updated macro code.
Common Mistakes
- Trying to run or edit VBA in Excel Online (the browser version). Macros are completely unsupported there—no warning, just silence or errors.
- Uploading a standard .xlsx file instead of .xlsm. Excel will strip VBA code silently if you save as .xlsx. Always confirm the file extension is .xlsm before uploading.
- Ignoring macro security settings. If your organization enforces Group Policy or Microsoft Defender Application Control, macros may be disabled by default. Contact your IT admin if you can’t enable them.
- Assuming OneDrive sync always updates the SharePoint version. While sync usually works, some network issues or offline editing can cause conflicts. Always verify in SharePoint after saving.
- Editing the file directly in SharePoint using the embedded Excel viewer. This only allows viewing, not editing, and never executes macros.
Tips
- Use Excel’s built-in macro recorder to generate starter code, then refine it manually. Record a simple task, view the code in the VBA editor, and adjust as needed.
- Include error handling in your VBA (e.g., On Error GoTo Handler) to prevent crashes and make debugging easier in a shared environment.
- Store sensitive logic in Personal Macro Workbooks (Personal.xlsm) only if running locally—don’t store them in SharePoint, as they’re user-specific and won’t sync reliably.
- Test macros on a copy of the file first, especially if they modify data. Upload the copy to a test SharePoint library before touching the live version.
- Disable auto-run macros (e.g., Auto_Open, Workbook_Open) during collaboration to avoid unexpected behavior when others open the file.
- Use clear comments in your VBA code. Future collaborators (or you, months later) will thank you when returning to the file.
Troubleshooting
If your macro doesn’t run or code disappears, follow these checks in order:
- Verify the file format. Right-click the file in SharePoint > Properties, and confirm the file type is "Excel Macro-Enabled Workbook (.xlsm)." If it shows "Excel Workbook (.xlsx)," re-upload the correct .xlsm version.
- Confirm you’re using desktop Excel. Look at the title bar: if it says "Excel - Web" or shows a browser URL, close it and open via "Edit in Excel" on your desktop.
- Check macro security. In Excel, go to File > Options > Trust Center > Trust Center Settings > Macro Settings. Ensure it’s set to "Disable all macros with notification" or "Enable all macros" (only temporarily for testing). Click OK and reopen the file.
- Check for VBA project corruption. In the VBA editor, go to Debug > Compile VBA Project. If errors appear, fix them before saving. If the Project Explorer shows a missing reference (red label), uncheck it under Tools > References.
- Test on a local file. Download the SharePoint file to your desktop, open it locally, and run the macro. If it works locally but not from SharePoint, the issue is likely sync or permissions.
- Review SharePoint permissions. If saving fails, you may lack Contribute rights. Ask a site admin to grant you Edit permissions on the library.
Frequently Asked Questions
Can I use VBA macros in Excel files stored in SharePoint Online?
Yes, but only when the file is opened in the desktop version of Excel (Excel for Microsoft 365, Excel 2019, etc.). Excel Online (browser) does not support VBA at all.
Why does my macro disappear after saving in SharePoint?
This happens if you save the file as .xlsx instead of .xlsm, or if you open and save it in Excel Online. Always use desktop Excel and verify the file extension before uploading.
Do all users need Excel installed to use a macro-enabled file in SharePoint?
No—anyone can view and interact with the file in the browser, but only users with desktop Excel and macro permissions can run or edit VBA code. Macros won’t execute for others using Excel Online.
Can I automate running a macro when the file is opened from SharePoint?
Yes, use the Workbook_Open event in the ThisWorkbook module. However, users must still enable macros when opening, and it only works in desktop Excel. Test thoroughly in your environment first.
Is VBA safe to use in shared SharePoint documents?
VBA is powerful but carries macro-based malware risks. Only enable macros from trusted sources. Your organization may restrict VBA via Microsoft Defender Application Control—check with IT if macros won’t enable.
What happens if two people edit the VBA code at the same time?
Excel locks the file when opened for editing. The second user will see the file as read-only until the first saves and closes it. There’s no real-time co-authoring for VBA code—always coordinate macro edits to avoid overwrites.