VBA Code to Open an Excel File from SharePoint

VBA & macros · Updated November 20, 2026

In Excel for Microsoft 365 on Windows, use VBA’s Workbooks.Open method with the file’s direct SharePoint URL, provided you are signed in and have permission to open it. For example, assign the URL of the workbook—not a sharing link—to a string and pass it to Workbooks.Open; if URL access is unreliable, open a copy from a SharePoint library synced to your computer with OneDrive.

Open a workbook with its SharePoint URL

This example opens an Excel workbook by its direct file address. Replace the sample tenant, site, library, and filename with the real values for your organization. Spaces in the URL path are written as %20. The example opens the file read-only and does not update links to other workbooks.

Option Explicit

Sub OpenSharePointWorkbook()
    Dim fileUrl As String
    Dim openedBook As Workbook

    On Error GoTo OpenError

    ' Replace this sample with the direct URL of your workbook.
    fileUrl = "https://contoso.sharepoint.com/sites/Finance/Shared%20Documents/Budget.xlsx"

    ' Open read-only; change ReadOnly to False if you need to edit the file.
    Set openedBook = Workbooks.Open( _
        Filename:=fileUrl, _
        UpdateLinks:=0, _
        ReadOnly:=True)

    MsgBox "Opened: " & openedBook.Name, vbInformation
    Exit Sub

OpenError:
    MsgBox "Excel could not open the SharePoint workbook: " & _
        Err.Description, vbExclamation
End Sub

Put the macro in a standard VBA module in the workbook you will use to launch it, or in an appropriately configured personal macro workbook. In Excel for Windows, open the Visual Basic Editor with Alt+F11, choose Insert > Module, and paste the code. Save the launcher as an Excel Macro-Enabled Workbook (.xlsm) if it contains the macro. Before running a macro, save a copy of the workbook: VBA changes cannot be undone with Ctrl+Z. This particular example opens the target read-only, but a macro can still alter other open workbooks or files if its code does so.

Find the correct SharePoint address

The URL must identify the workbook itself. A normal direct address commonly has the form https://tenant.sharepoint.com/sites/SiteName/LibraryName/FolderName/FileName.xlsx, though the exact site and library names depend on how the organization set up SharePoint. A library displayed as “Documents” in the browser may have a different internal name in its URL. Copying a link from SharePoint and using it without checking its format can therefore lead to errors.

A sharing link is not necessarily a direct file URL. Links containing patterns such as /:x:/r/, a sharing token, or parameters such as ?e= are designed for sharing or browser navigation. They may open in a browser but fail when passed to Workbooks.Open. If Excel reports that it cannot find the file, navigate to the workbook in the SharePoint library and obtain its actual path or use OneDrive sync instead. Avoid guessing the library path: sites can have nested folders, renamed libraries, and organization-specific URLs.

Use the address for the workbook, not the SharePoint site homepage, a folder page, or a browser URL that ends in a viewer parameter such as ?web=1. Excel needs a path it can resolve to the file. If a path includes characters that have special meaning in a URL, make sure they are correctly encoded. Do not encode the entire URL indiscriminately; the scheme and separators such as https:// and / must remain usable.

How Excel handles sign-in and permissions

VBA does not sign in to SharePoint just because it supplies a URL. Excel generally relies on the Microsoft 365 account already signed in to Office and the access that account has to the site. Before running the macro, open the workbook manually from SharePoint in desktop Excel, confirm that the right work account is active, and complete any organization-required sign-in or multifactor authentication prompts. If the account lacks permission, VBA cannot grant access.

Office may show a sign-in, access, or confirmation prompt when opening a cloud file. Do not treat the URL as a way around your organization’s authentication policies. If the file works in a browser but not in desktop Excel, check whether the browser and Office are using different accounts, whether the link is only a browser-sharing link, and whether the file is available to open in the desktop application. An organization can also apply policies that affect how Office connects to SharePoint.

For a workbook you intend to edit, omit ReadOnly:=True or set it to False, subject to the file’s permissions and any lock held by another user. For automated reporting, read-only access can be safer because it helps prevent accidental changes. The UpdateLinks:=0 argument suppresses updating external workbook links during opening; it does not disable every connection, query, or refresh operation in the workbook.

Use a OneDrive-synced library as an alternative

When direct URL opening is inconsistent, syncing the SharePoint document library with the OneDrive desktop app is often a practical alternative. In SharePoint, open the document library and choose its Sync option, then follow the OneDrive prompts. Once synchronization is set up, the library appears in File Explorer on Windows. You can open the workbook from that local-looking path, and OneDrive handles synchronization with SharePoint.

This approach can be helpful when a macro must work with ordinary file paths, when the direct URL is difficult to construct, or when users need to choose among files. It does depend on the library being synced on that computer and on the file being available locally or accessible through OneDrive. Files marked as online-only may need to download before Excel can open them, so the first open can take longer and may require a network connection.

Do not hard-code a user-specific path such as C:\Users\Alex\ if other people will run the macro. Each person may have a different Windows profile name, OneDrive location, or organization folder name. A more maintainable option is to let the user select the synced workbook with Excel’s file picker, then pass the chosen path to Workbooks.Open. The path still needs to exist on that computer.

Let the user choose a synced workbook

This alternative displays a file picker and opens the selected workbook read-only. It selects a file from the computer’s available folders; it does not browse SharePoint online or make a library sync automatically. Users should navigate to the synced SharePoint library in the picker.

Option Explicit

Sub ChooseAndOpenSyncedWorkbook()
    Dim selectedFile As Variant
    Dim openedBook As Workbook

    selectedFile = Application.GetOpenFilename( _
        FileFilter:="Excel workbooks (*.xlsx;*.xlsm),*.xlsx;*.xlsm", _
        Title:="Select a synced SharePoint workbook")

    If selectedFile = False Then Exit Sub

    On Error GoTo OpenError
    Set openedBook = Workbooks.Open( _
        Filename:=CStr(selectedFile), _
        UpdateLinks:=0, _
        ReadOnly:=True)

    MsgBox "Opened: " & openedBook.Name, vbInformation
    Exit Sub

OpenError:
    MsgBox "Excel could not open the selected workbook: " & _
        Err.Description, vbExclamation
End Sub

The file filter in this example includes .xlsx and .xlsm workbooks. Add other formats if your workflow requires them, but be cautious with macro-enabled files from untrusted sources. A file picker can reduce path-typing errors; it does not confirm that the selected file is the correct version or that it is safe to run macros inside it.

Choosing between a URL and a synced path

Use a direct URL when the address is stable, the user is already authenticated in Office, and the same cloud file should be opened for everyone. Use a synced path when the file picker is more suitable, the URL format causes trouble, or the macro needs to work with standard file-system operations. Neither method is universally better: the right choice depends on authentication, how users access the library, and whether the macro is shared across computers.

  • Direct URL: avoids relying on each person’s local folder layout, but requires a valid direct file address and working Office authentication.
  • Synced path: works like a familiar local file path, but depends on OneDrive sync being configured and the path being available on that computer.
  • Manual browser opening: useful for checking access or locating a file, but a browser sharing link may not be a usable VBA filename.

For a macro distributed to several people, test the intended method with each person’s account and computer setup. A URL can be consistent while permissions differ; a synced path can work well for one user but point somewhere else for another. If your process requires unattended automation, ask your Microsoft 365 administrator about approved authentication and automation options rather than storing passwords in VBA.

Common errors and practical fixes

When the macro fails, the error message is only a starting point. Try opening the same workbook manually in desktop Excel with the same account. That comparison helps distinguish a bad VBA path from a sign-in, permission, network, or file issue.

  • “File not found” or a similar path error: verify the site, library, folder, filename, extension, and URL encoding. Make sure you supplied a direct file address rather than a folder or sharing page.
  • Sign-in or access denied: confirm that Excel is signed in with the account that has permission. Ask the site owner for access if necessary; changing VBA code does not create SharePoint permissions.
  • The link opens in a browser but not Excel: it may be a browser-oriented sharing link. Try the library’s actual file path or sync the library and open the file by its synced path.
  • The workbook opens read-only: the example explicitly requests read-only access. Remove that argument if editing is required, then check permissions, coauthoring state, and whether another process has the file locked.
  • It works on one computer only: check for a hard-coded local path, different Office accounts, different library sync settings, or different access rights.

Use a clear error handler, as in the examples, so the user sees a useful message instead of a macro stopping without explanation. For troubleshooting, you can temporarily display the value of fileUrl or selectedFile before the open call and compare it with the actual workbook location. Do not display or log authentication tokens if a copied sharing link contains them.

Windows, Mac, and Excel for the web

The examples target desktop Excel for Microsoft 365 on Windows. Excel for the web does not run VBA macros, so a workbook opened in a browser cannot execute these procedures there. The macro must run in desktop Excel. The Windows shortcut for the Visual Basic Editor is Alt+F11; on Mac, the shortcut is typically Option+F11, and some keyboards require the Fn key as well. Ribbon and keyboard behavior can vary with macOS and keyboard settings.

Excel for Mac supports VBA, but file paths use macOS conventions, OneDrive sync locations differ, and cloud sign-in or URL behavior can differ from Windows. Do not assume that a Windows path or Windows-specific API will work on Mac. If a macro must support both platforms, test it in the relevant desktop versions and consider selecting a synced file rather than embedding a Windows path. The file picker example uses Excel’s VBA interface, but the selected path is still platform-specific.

Frequently Asked Questions

Can VBA open a SharePoint file directly?
Yes, Excel VBA can pass a direct SharePoint workbook URL to Workbooks.Open when the URL is valid and the signed-in Office account has access. A browser sharing link is not always a direct file URL.

Why does my SharePoint link work in a browser but not in VBA?
The link may lead to a browser viewer or sharing page rather than the workbook file itself. It can also use an account or permission that is not active in desktop Excel. Try the direct library file address or a OneDrive-synced path.

Can I open a SharePoint workbook without signing in?
Not if the SharePoint site requires authentication. VBA does not bypass sign-in or permissions. Use an account authorized for the file and follow your organization’s access policies.

How do I open the workbook so I can edit it?
Remove ReadOnly:=True or set it to False in the open call. Editing still depends on your SharePoint permissions and whether the workbook can be opened for editing at that time.

Can this macro run in Excel for the web?
No. Excel for the web can open SharePoint workbooks, but it does not execute VBA. Run the macro in desktop Excel for Windows or Mac.

Should I put my SharePoint password in the VBA code?
No. Do not store passwords or authentication tokens in a macro. Use the Office sign-in experience and access methods approved by your organization.

Related

Try it: the Column Letter to Number: aA is 27, XFD is 16,384. Converts both ways.

More in VBA & macros

Sources: Function behaviour and menu paths: Microsoft Support documentation for Excel. Method & sources

Spotted an error in this guide? Let us know.