Use Excel VBA to Open a SharePoint File with a Changing Name
Question details
Open a SharePoint workbook whose name changes and transfer a range of values into a fixed workbook using VBA.

- Product
- Microsoft Excel VBA and SharePoint
- Device & OS
- not provided
- Scenario
- A macro needs to open a changing source workbook, copy its data, and write values to the workbook containing the macro.
- Observed behavior
- The macro needs reliable workbook references so that changing file names or the active window do not redirect its operations.
Use a desktop version of Excel that supports VBA, sign in to SharePoint, and confirm you can open the source workbook directly. Save the destination as a macro-enabled workbook and identify the source and destination sheet names and ranges.
Open the SharePoint workbook using a URL stored in a cell
Use this approach when you can update a cell with the current source file's direct URL before running the macro.
Workbooks.Open returns a Workbook object that the macro can use regardless of the active window. ThisWorkbook refers to the workbook containing the macro, so the destination does not depend on which workbook is active.
In the macro workbook, create a sheet named Control and enter the source workbook's direct SharePoint file URL in cell B1. Update this cell whenever the file name changes. Use a URL Excel can open directly, not a sharing-page link.
Open the VBA editor with Alt+F11, choose Insert > Module, and paste the following code. Change the sheet names and ranges to match your workbooks: Sub ImportSharePointValues() Dim sourceBook As Workbook Dim sourceUrl As String Dim errorMessage As String On Error GoTo HandleError sourceUrl = Trim$(ThisWorkbook.Worksheets("Control").Range("B1").Value) If sourceUrl = "" Then Err.Raise vbObjectError + 1, , "Enter the source URL in Control!B1." Set sourceBook = Workbooks.Open(Filename:=sourceUrl, ReadOnly:=True) ThisWorkbook.Worksheets("Destination").Range("A1:C10").Value2 = _ sourceBook.Worksheets("Source").Range("A1:C10").Value2 sourceBook.Close SaveChanges:=False Exit Sub HandleError: errorMessage = Err.Description On Error Resume Next If Not sourceBook Is Nothing Then sourceBook.Close SaveChanges:=False On Error GoTo 0 MsgBox "Import failed: " & errorMessage, vbExclamation End Sub
Close the source workbook if it is already open, then run ImportSharePointValues from Excel's Developer > Macros menu. Check Destination!A1:C10 for the imported values.

Find the current file in a SharePoint-synced folder
Use this approach when the SharePoint library is synced locally and the changing file names follow a predictable pattern.
Work with Excel-compatible files in WPS Office
WPS Office is a free, lightweight alternative with a familiar interface for opening and editing spreadsheet files. Its Microsoft Office format compatibility helps you move existing files into WPS, though it does not fix an Excel VBA or SharePoint automation issue.

Frequently Asked Questions
Why should I avoid ActiveWorkbook in this macro?
ActiveWorkbook can change when another workbook or window becomes active. Store the result of Workbooks.Open in a Workbook variable, and use ThisWorkbook for the workbook containing the macro.
How do I update the macro when the SharePoint file name changes?
Enter the current file's direct URL in Control!B1 before running the example. The macro reads that cell and passes its value to Workbooks.Open.
Can VBA automatically find the newest matching SharePoint file?
It can, but the macro needs an explicit way to list files and compare dates. For a locally synced library, search the folder using Dir and apply a defined date-based selection rule; do not assume the first match is the newest.
Why won't Workbooks.Open open my SharePoint link?
Check that you are signed in and have permission to access the file. Test the direct file URL in Excel and avoid using a link that opens only a SharePoint sharing page.
Does the example copy formulas or formatting?
No. The Value2 assignment transfers the source range's values into the destination range. It does not copy formulas or formatting.




