How to Use VBA to Write Data to an Excel File Stored in SharePoint
Question details
The user needs to know if and how VBA can be used to write data into an Excel file hosted on SharePoint, while avoiding file locks and syncing conflicts.

- Product
- Excel
- Device & OS
- not provided
- Scenario
- Automating data entry into a shared workbook hosted on SharePoint using VBA scripts.
- Observed behavior
- Simultaneous desktop edits and direct VBA write operations to SharePoint URLs can cause file locks, data conflicts, overwritten changes, and unreliable reporting.
Ensure you have the appropriate edit permissions for the SharePoint document library and that your OneDrive desktop client is installed and actively syncing if you plan to use local file paths.
Synchronize the SharePoint Library to a Local OneDrive Folder
The most reliable way for VBA to interact with a SharePoint file is by syncing the document library to your local machine using OneDrive.
By syncing the SharePoint folder locally, your VBA code can treat the file as a standard local document. The OneDrive client handles the cloud synchronization in the background, significantly reducing the complexity of the VBA code and minimizing authentication errors.
Navigate to your target document library in SharePoint Online using your web browser. Click the 'Sync' button in the top menu bar to open OneDrive and sync the folder to your PC.
Open Excel, and press Alt + F11 to launch the VBA Editor.
Update your VBA script to reference the local OneDrive synced file path instead of the HTTPS SharePoint URL (e.g., C:\Users\[Username]\[Organization]\[SharePoint Folder]\file.xlsx).
Execute your macro to write the data. OneDrive will automatically detect the changes and upload the modified file back to SharePoint.

Use the Direct SharePoint URL Path in VBA
You can use the direct HTTPS SharePoint path in your VBA code, though this method requires careful handling of file locks.
Run VBA Macros on Synced SharePoint Files with WPS Office
WPS Office fully supports VBA macros and allows you to read, write, and manipulate Excel files (.xlsm). By syncing your SharePoint library locally, you can use WPS Spreadsheets to execute VBA scripts seamlessly and efficiently.
- 1. Download and install WPS Office: Download the latest version of WPS Office and ensure you have the VBA module enabled.
- 2. Sync your SharePoint folder: Use the OneDrive desktop client to sync your target SharePoint document library to your local PC.
- 3. Open your macro workbook: Open your macro-enabled workbook (.xlsm) using WPS Spreadsheets.
- 4. Access the VBA Editor: Go to the Tools tab and click 'Macro', or press Alt + F11 to open the WPS VBA Editor.
- 5. Run your data script: Run your macro using the local synced path to write data to the target file effortlessly.

Frequently Asked Questions
Why does my VBA script get an access error when writing to a SharePoint URL?
This usually occurs because VBA cannot handle modern web authentication (like MFA) directly, or because the file is checked out or locked by another user. Syncing the folder locally via OneDrive bypasses most URL authentication issues.
Can multiple users run the VBA write script at the same time?
No, simultaneous desktop edits via VBA can cause file locks, conflicts, and overwritten changes. If concurrent data collection is required, it is best to use a database, Dataverse, or a SharePoint List instead of a single Excel file.
How do I handle file locks in my VBA code?
You can add error handling in your VBA script to check if the file is locked before writing. Use a loop structure that waits a few seconds and retries the save operation if it detects a read-only or locked status on the file.
Can I write directly to a SharePoint List instead of an Excel file using VBA?
Yes, you can use ActiveX Data Objects (ADO) in VBA to connect directly to a SharePoint List and perform write operations. This method is much more robust and reliable for shared data collection than writing to a shared Excel workbook.




