Yes, VBA can save an Excel workbook straight into a SharePoint folder, and the simplest reliable way is to save it to the folder synced to your PC by OneDrive for work or school, then let the sync client upload it. In code terms you point ThisWorkbook.SaveAs or SaveCopyAs at the local synced path, for example C:\Users\YourName\YourCompany\Team Site - Documents\Reports\Summary.xlsx. That path is the real URL behind the scenes, so no authentication code or web request is needed. Saving directly to an https:// address works only in some versions and is far more fragile, so treat the synced route as your default and the web address as a last resort.
What You'll Need
- Excel on Windows with the folder already synced by OneDrive for work or school (the client that syncs SharePoint document libraries). Excel for Mac has no OneDrive for work or school sync client in the same form, so Mac users should read the notes in the FAQ.
- Edit or Contribute permission on the SharePoint document library, plus permission to create files in the target folder.
- The synced folder present in File Explorer, with the green tick or cloud icon showing it is fully in sync.
- The macro-enabled workbook saved as .xlsm, and the VBA project allowed through Trust Center macro settings.
- A short, plain folder and file name with no characters Windows forbids:
\ / : * ? " < > |.
Menu and dialog labels can differ slightly by Excel version, Windows build and region, so match by position and wording rather than by exact text.
Step-by-Step
- Confirm the synced path. Open File Explorer, find the SharePoint library under your organisation's name in the left pane, open the target folder, then click the address bar and copy the full path. It ends in something like
...\Team Site - Documents\Reports. This is the string your macro must use. - Open the macro editor. In Excel press
Alt+F11to open the Visual Basic Editor, then use Insert > Module to add a standard module. - Write the save routine. Type a procedure that builds the destination path from the folder plus a file name, then saves. A typical pattern is: set a
Stringvariable to the synced folder, build the full path with the file name and.xlsxextension, check it withDirif you need to avoid overwriting, then callThisWorkbook.SaveCopyAs fullPathif you want to keep the open file unchanged, orThisWorkbook.SaveAs fullPath, FileFormat:=xlOpenXMLWorkbookif you want to switch the active file to the saved one. - Handle a timestamped copy. For daily or per-run files, build the name with
Format(Now, "yyyy-mm-dd_hh-nn")and append it before the extension. Use hyphens, not colons, because colons are illegal in Windows file names. - Turn off prompts and screen updates briefly. Wrap the save in
Application.DisplayAlerts = FalseandApplication.ScreenUpdating = False, and set both back toTrueat the end, including in an error handler, so Excel does not sit on an invisible dialog. - Run the macro and watch the sync icon. Press
F5in the editor or run it from the Macro dialog. Within seconds the file should appear in the folder, and the OneDrive icon in the notification area shows it uploading. A cloud icon means it is queued; a green tick means it is on the server. - Verify on the web. Open the SharePoint library in a browser and confirm the file is there with the expected modified time. If it is, the macro is finished.
- Trigger it automatically if you want. Add a call to your routine from
Workbook_BeforeCloseor a button on the sheet, so the copy is saved without anyone opening the editor.
Common Mistakes
- Hardcoding a personal path. A path with your own username fails the moment a colleague runs the macro. Store the folder in a named cell, a constant at the top of the module, or a small settings sheet so it can be changed in one place.
- Saving to the https:// URL. Excel sometimes accepts a web address for
SaveAsand sometimes returns an error or opens a sign-in prompt, and behaviour varies between versions and tenants. Prefer the synced path. - Forgetting the file extension. Passing a name without
.xlsxleaves Excel guessing the format and can trigger a compatibility prompt, which stalls an unattended macro. - Leaving DisplayAlerts off. If an error occurs before the reset line, every later save in that Excel session happens silently and can overwrite files without asking.
- Assuming the upload already finished. The file exists locally the instant the macro returns, but the upload may still be pending. Code that immediately emails the file to someone should pause or point them at the SharePoint link instead.
- Using illegal characters in generated names. Dates, times and user names can contain
:,/or?, which makesSaveAsfail with a path error. - Files opened exclusively by someone else. If a colleague has the same target workbook open, an overwrite can fail or create a conflict copy.
Tips
- Keep OneDrive for work or school running and signed in; without it the synced folder is just a local folder and nothing reaches SharePoint.
- Right-click the synced folder and use the option to copy the local path, so the string you paste into VBA is exact.
- Use
SaveCopyAsfor archives so the user's open workbook keeps its own name and unsaved edits. - Write a short status line to the sheet or an Immediate window message after each save, with the full path. When something goes wrong, that line tells you instantly which path was attempted.
- Prefer a helper function that checks the folder exists with
Dir(path, vbDirectory)before saving, and creates nothing automatically if it is missing: a missing folder usually means sync is broken or permissions changed, and creating a local folder of the same name hides the real problem. - Test with a throwaway workbook and a test subfolder before pointing the macro at a live library.
Troubleshooting
If the macro errors with "path not found" or "could not be found", first confirm the synced folder still exists in File Explorer and is syncing (check the OneDrive icon and any error messages in its activity window). Next check the path string character by character against the address bar, including spaces and hyphens. Then confirm you have permission to add files in that library: try creating a file there by hand.
If the file saves locally but never appears on the SharePoint site, the sync client is the culprit. Pause and resume syncing from the OneDrive menu, sign out and back in, and look at the sync activity for errors about blocked file types or the library being read-only. Libraries can be set to block certain extensions, and a blocked file sits as an error rather than uploading. Check with whoever administers the site.
If you get a permission or sign-in prompt, close Excel, sign out of all Office accounts and sign in again (File > Account), then reopen the workbook. Persistent prompts usually mean the site is not synced on this PC at all, so add the library to your sync again from the SharePoint site using the Sync button.
If an overwrite fails, check whether the target file is open elsewhere or has been checked out. Ask the person to close it, or change your macro to write timestamped file names so nobody is overwritten.
If nothing happens at all, check that macros are enabled (File > Options > Trust Center > Trust Center Settings > Macro Settings) and that the workbook was saved as .xlsm. As a last resort, restart Excel and then the PC before changing code.
Frequently Asked Questions
Can VBA save directly to a SharePoint URL without syncing?
Sometimes, but it is unreliable. Excel may accept an https:// address for SaveAs in some versions and fail or prompt for credentials in others. The synced local path is the dependable method.
Does this work on a Mac?
Not in the same way. Mac Excel has no OneDrive for work or school sync client that exposes the library as a local folder, so the synced-path approach does not apply. Check with your organisation about supported options.
Will other people see the file immediately?
Only after the upload completes. The local file exists instantly, but colleagues reading the SharePoint library see it once sync has pushed it to the server, which is usually seconds but can be longer on a poor connection.
How do I stop the macro overwriting an existing file?
Test the path with Dir first and either exit, add a suffix, or use a timestamp in the name. Do not rely on a confirmation prompt, because prompts freeze unattended runs.
Why does it save into my personal OneDrive instead?
Because the path you used is the personal OneDrive folder. Personal and work/school OneDrive folders live in different locations in File Explorer, so copy the address from the work library itself.