To open a SharePoint-hosted Excel file using Excel VBA, you must first ensure the file path is accessible via a UNC or HTTP(S) URL (e.g., https://yourcompany.sharepoint.com/sites/ProjectX/Shared%20Documents/Report.xlsx), and your Excel instance has the correct permissions and trust settings. VBA can open the file directly using Workbooks.Open with the full URL, but requires proper authentication (e.g., signed-in Microsoft account in Excel) and network access. No additional libraries are needed beyond standard Excel VBA.

What You'll Need

  • A valid SharePoint URL for the Excel file (including .xlsx extension)
  • Excel installed (desktop version, not Online-only)
  • Active internet connection and access to the SharePoint site
  • Logged-in Microsoft account in Excel with permissions to the file
  • Excel Trust Center settings permitting macro execution

Step-by-Step

  1. Open Excel and press Alt + F11 to launch the Visual Basic for Applications (VBA) editor.
  2. In the VBA editor, go to Insert > Module to create a new blank module.
  3. Copy and paste the following VBA code into the module, replacing the placeholder URL with your actual SharePoint file path:
Sub OpenSharePointExcel()
    Dim wb As Workbook
    Dim sharePointUrl As String
    
    ' Replace with your SharePoint file's full URL
    sharePointUrl = "https://yourcompany.sharepoint.com/sites/ProjectX/Shared%20Documents/Report.xlsx"
    
    On Error GoTo ErrorHandler
    
    Set wb = Workbooks.Open(sharePointUrl)
    
    MsgBox "File opened successfully: " & wb.Name, vbInformation
    Exit Sub
    
ErrorHandler:
    MsgBox "Error opening file. Check URL, permissions, or network connection." & vbCrLf & _
           "Error " & Err.Number & ": " & Err.Description, vbCritical
End Sub
  1. Ensure the SharePoint URL uses URL-encoded characters (e.g., spaces become %20, ampersands %26). For example, a folder named "Q3 Report" should appear as Q3%20Report in the path.
  2. Save the workbook as a macro-enabled file (.xlsm) by going to File > Save As > Excel Macro-Enabled Workbook.
  3. Close the VBA editor and return to Excel. Press Alt + F8, select OpenSharePointExcel, and click Run.
  4. Excel will attempt to download and open the file. If prompted, sign in with your work or school account if not already authenticated.

Common Mistakes

  • Using a browser-style link (e.g., https://.../Forms/AllItems.aspx?id=...) instead of the direct file URL. The direct URL must end with .xlsx and not include query parameters like ?web=1.
  • Missing URL encoding for special characters in folder or file names (e.g., & should be %26).
  • Running the code in Excel Online—VBA macros are not supported in browser-based Excel, so the desktop app is required.
  • Assuming the file path from Windows Explorer works: SharePoint files often use a mapped drive (e.g., Z:\), but VBA may fail to resolve it. Use the full HTTPS URL instead.
  • Not signing in to Excel with the correct account. If Excel is signed in with a personal Microsoft account but the SharePoint site is work/school-based, authentication will fail.

Tips

  • Test the URL first by pasting it directly into your browser’s address bar. If it opens the file in Excel Online, the URL is likely correct.
  • For files in subfolders, copy the URL from the browser’s address bar while the file is open in Excel Online (click Open in Excel first if needed), then paste it into VBA.
  • Use Debug.Print sharePointUrl in the VBA editor to verify the exact URL being used before running the procedure.
  • For recurring files, store the SharePoint base URL in a named cell or configuration sheet to avoid hard-coding paths.
  • Enable Trust access to the VBA project object model in File > Options > Trust Center > Trust Center Settings > Macro Settings if your code needs to interact with other Excel features programmatically.

Troubleshooting

If your VBA code fails to open the SharePoint Excel file, follow these steps in order:

  1. Check network and login status: Ensure you are connected to the internet and signed into Excel with the same account that has access to the SharePoint site. Try opening the file manually in Excel (File > Open > Browse, then paste the URL) to verify access.
  2. Verify the URL format: Double-check for typos, unencoded spaces (%20), or invalid characters. Remove any trailing spaces or extra slashes. A correct URL looks like: https://company.sharepoint.com/sites/Sales/Shared%20Documents/SalesQ1.xlsx.
  3. Check file permissions: Confirm you have at least Edit permissions on the file. If you only have View access, Workbooks.Open may still work, but saving will fail—ensure your code doesn’t try to save without write access.
  4. Disable add-ins temporarily: Some Excel add-ins interfere with network requests. Press Alt + F11, close the VBA editor, then go to File > Options > Add-ins, uncheck all, restart Excel, and try again.
  5. Clear cached credentials: Open Windows Credential Manager > Windows Credentials, remove any entries starting with MicrosoftOffice16_Data: or SPHTTP:, then restart Excel and sign in again.
  6. Check Trust Center settings: Go to File > Options > Trust Center > Trust Center Settings > Macro Settings and ensure Enable all macros (not recommended for production) or at least Disable all macros with notification is selected. Also confirm Enable insecure active X controls is unchecked unless required by your IT policy.
  7. Try the file via UNC path: In Windows File Explorer, navigate to the SharePoint document library (e.g., via Open with Explorer in the library’s ribbon), then copy the full path (e.g., \\company.sharepoint.com@SSL\DavWWWRoot\sites\ProjectX\Shared Documents\Report.xlsx) and use that in VBA. Note: This method only works on Windows with WebDAV enabled.

If none of these resolve the issue, contact your SharePoint administrator to verify that Anonymous Access is not required (it’s disabled by default and unsupported for direct file access via VBA) and that your organization hasn’t blocked programmatic access via conditional access policies.

Frequently Asked Questions

Can I open a SharePoint Excel file in Excel Online using VBA?

No. VBA macros are not supported in Excel Online—only the desktop Excel application (on Windows or Mac) supports VBA. You must use the desktop app to run this code.

Do I need special permissions or SharePoint admin rights?

No. You only need standard Edit or Contribute permissions on the SharePoint file. Admin rights are not required unless your organization enforces strict network or security policies that block programmatic access.

Why does the file open as read-only even though I have edit access?

This usually happens if the file is already checked out or locked by another user. SharePoint locks files when someone opens them for editing. Wait a few minutes and retry. If the lock persists, ask the user who has it open to save and close it, or contact an admin to force a checkout release.

Can I open a SharePoint file without hard-coding the URL in VBA?

Yes. Store the SharePoint URL in a named cell (e.g., name cell A1 as SharePointUrl), then replace the hard-coded line with: sharePointUrl = ThisWorkbook.Names("SharePointUrl").RefersToRange.Value. This makes your macro more portable across environments.

Does this work with OneDrive for Business files?

Yes, but use the OneDrive for Business URL format: https://yourcompany-my.sharepoint.com/personal/username_domain_com/Documents/Report.xlsx. Ensure the URL is correct and that the file hasn’t been moved to a different OneDrive location.

Is there a way to automatically save changes back to SharePoint after editing?

Yes. After opening the workbook with Workbooks.Open, use wb.Save to push changes back to SharePoint. No extra steps are needed—the file is already linked to its SharePoint location. Avoid using SaveAs unless you intend to create a local copy, as that detaches it from SharePoint.