Fix Macro Errors When Running Excel Files from Synced SharePoint Folders
Question details
Users encounter a 'file not found in the same folder' error when trying to run macro-enabled Excel files from a locally synchronized SharePoint folder, even though the macros work perfectly when stored completely locally.

- Product
- Microsoft Excel
- Device & OS
- not provided
- Scenario
- Executing VBA macros from an Excel workbook (.xlsm) that is stored in a locally synced SharePoint or OneDrive folder via File Explorer.
- Observed behavior
- The macro execution fails and throws a file not found error. This typically happens because the VBA code expects a local file path, but the synchronized folder environment resolves dependent paths differently.
Verify that all dependent workbooks are synchronized to the same local folder and that macros are enabled in your Excel Trust Center settings.
Create a User Environment Variable for the Excel Document
Setting up a dedicated environment variable can help the system correctly resolve the local path for the synced workbook.
This is a proven workaround for vendor-created workbooks where VBA code fails to resolve the correct local directory path in synchronized SharePoint or OneDrive environments. You will need to apply this variable for each user who accesses the file.
Press the Windows key, type 'Environment Variables', and select 'Edit environment variables for your account' from the search results.
In the Environment Variables window, click the 'New' button under the 'User variables' section.
Enter a descriptive Variable name (e.g., 'SharePointSyncPath') and set the Variable value to the exact local folder path where your SharePoint files are synchronized. Click 'OK' to save.
Ensure your VBA code uses the Environ() function to retrieve this path, or configure the workbook to reference this environment variable instead of a hardcoded string.

Sync the Entire Directory and Fix VBA Path References
Ensure all dependent files are downloaded locally and update the VBA code to dynamically locate them.
Try WPS Office for Seamless Macro and VBA Support
If you continue to experience file path synchronization issues with Microsoft Excel and SharePoint, consider switching to WPS Office. It provides robust support for VBA and macro-enabled documents, offering a lightweight, familiar, and highly compatible alternative.
- 1. Download WPS Office: Visit the official WPS Office website and click the free download button.
- 2. Install the Software: Run the downloaded installer file and follow the on-screen instructions.
- 3. Open Your Macro-Enabled Files: Launch WPS Spreadsheets and open your .xlsm files to run your VBA macros seamlessly.

Frequently Asked Questions
Why do my Excel macros work locally but not in a synchronized SharePoint folder?
When files are synced via OneDrive or SharePoint, Excel sometimes resolves the file path as a web URL (https://) instead of a local drive path (e.g., C:\). If your VBA code is designed to locate files in a local directory, it cannot read the cloud URL, triggering a 'file not found' error.
Can I run macro-enabled Excel files directly in the SharePoint web version?
No, macros are not supported in the web version of SharePoint or Excel Online. You must open the document in the desktop application to run or edit VBA code.
How can I prevent OneDrive from changing my local VBA paths to URLs?
You can turn off the 'Use Office applications to sync Office files that I open' setting in your OneDrive client, though this may affect real-time co-authoring. Alternatively, update your VBA code with a function that automatically translates the OneDrive URL back to the local path.




