Excel VBA: How to Update Workbook Links
In this article
To update workbook links from Excel VBA, set each external link to update automatically or refresh it on demand with Workbook.UpdateLink, or let Excel prompt and update everything at once with Application.AskToUpdateLinks and Workbook.UpdateLink Name:=ActiveWorkbook.LinkSources. For a file that opens with stale numbers, the shortest fix is Workbooks.Open with UpdateLinks:=3, which refreshes external references without a dialog. The rest of this guide shows exactly how those lines should be written, where to put them, and what to do when a link will not resolve.
What You'll Need
- Excel on Windows or Mac with the Visual Basic Editor available (on Windows, press Alt+F11 to open it; on Mac, use Tools > Macro > Visual Basic Editor from the ribbon). Menu labels can differ slightly by version and platform.
- Macro-enabled workbooks: the file containing your code must be saved as .xlsm, .xlsb or .xlam. A plain .xlsx cannot store the macro.
- The linked workbooks open, or reachable at the path recorded in the link. A link to a file on a disconnected network share will fail no matter what the code says.
- Macro security set to allow macros (Developer tab > Macro Security), or the workbook placed in a Trusted Location. Only enable macros in files you created or trust.
- Basic familiarity with reading and editing a short block of VBA in the editor.
Step-by-Step
1. Find out what links the workbook actually has
- Open the workbook that contains the external references.
- In the Visual Basic Editor, open the Immediate Window (Ctrl+G on Windows).
- Type this line and press Enter:
For Each v In ActiveWorkbook.LinkSources(xlExcelLinks): Debug.Print v: Next v - Each line printed in the Immediate Window is a source file path. If nothing prints, the workbook has no Excel-type links and any update code will do nothing.
- You can also see the same list in the UI: Data tab > Edit Links (called Workbook Links in some newer builds). That dialog also confirms whether each link is set to automatic or manual.
2. Update all links in the open workbook
- Insert a normal module: in the editor, Insert > Module.
- Paste a short procedure. The core call is
ActiveWorkbook.UpdateLink Name:=ActiveWorkbook.LinkSources, which updates every Excel link in one pass. - Add
Application.DisplayAlerts = Falsebefore the call and set it back toTrueafterwards so Excel does not pause on a prompt. - Wrap the block in
On Error GoTohandling if you want a message instead of a crash when a source is missing. - Save the workbook, then run the macro (Developer tab > Macros, or F5 in the editor with the cursor inside the procedure).
3. Update a single named link only
- Use
UpdateLinkwith one path instead of the array:ActiveWorkbook.UpdateLink Name:="C:\\Reports\\Sales2026.xlsx", Type:=xlExcelLinks. - The path must match the stored link exactly, including the file extension and backslashes on Windows. On Mac, paths use forward slashes and often start with a volume name.
- Loop over
LinkSourcesand update selectively if some sources are slow network files you want to leave alone.
4. Update links when the file is opened by code
- The
Workbooks.Openmethod accepts anUpdateLinksargument. - Use
UpdateLinks:=0to open without updating,UpdateLinks:=3to update all external references, andUpdateLinks:=1or2to update only external or only remote references respectively. - Omitting the argument causes the familiar prompt, which is not what you want in an automated routine.
- Example:
Workbooks.Open Filename:="C:\\Reports\\Summary.xlsx", UpdateLinks:=3.
5. Control the prompt when the workbook is opened by a user
- Set
Application.AskToUpdateLinks = Falseearly in your open or startup routine to stop the dialog appearing. - Set it back to
Truewhen your routine finishes, otherwise other workbooks that session will open silently too. - Remember this setting only affects the current Excel session; it does not permanently change the workbook.
6. Change a link that points to a file that moved
- Use
ActiveWorkbook.ChangeLink Name:="C:\\Old\\Sales.xlsx", NewName:="C:\\New\\Sales.xlsx", Type:=xlExcelLinks. - Update the link immediately afterwards so the values refresh against the new file.
- If the source workbook has been replaced by a copy with the same name, this is usually unnecessary; Excel follows the earlier path.
7. Test, then save
- Run the macro on a copy of the file first. Link updates overwrite cached values, and a misdirected update can write wrong figures into summaries.
- Check a few known cells against the source file to confirm the numbers refreshed.
- Save the workbook (Ctrl+S) so the new cached values and link states persist; otherwise the next open will start from the old set.
Common Mistakes
- Confusing link types.
LinkSources(xlExcelLinks)lists only links to other workbooks. Links to Access or other data sources use a different type and need the matching argument. - Leaving alerts off. If the macro errors after setting
DisplayAlerts = Falseand you do not reset it, Excel stays silent about real problems for the rest of the session. - Assuming UpdateLink recalculates everything. It refreshes external references, not formulas depending on unsaved changes in the linked file. Save the source workbook first.
- Hard-coding paths. Paths with spaces, mapped drives that differ per user, or OneDrive and SharePoint locations can break. Prefer building the path from a known folder when you can.
- Running from the wrong workbook.
ActiveWorkbookpoints at whatever has focus; use an explicit workbook object in longer routines. - Trusting a saved .xlsx. If the code disappears, the file was probably saved without the macro.
Tips
- Set links to automatic in the Edit Links dialog for files that update often; the code then has less to do.
- Keep one reusable procedure that takes a workbook as an argument rather than repeating the same two lines in every file.
- Log the link list and the time of the update to a sheet so you can prove when data was last refreshed.
- For shared workbooks, update all links once in a controlled session instead of letting dozens of users trigger refreshes on network files.
- Keep a copy of the original figures before any bulk update; cached values are gone after a successful refresh.
Troubleshooting
The update does nothing. Check that LinkSources actually returns paths. If it prints nothing, the references are not Excel workbook links, so the wrong type is being updated or there is nothing to refresh.
No prompt appears and values stay old. The link may be set to manual in the UI and you have not called UpdateLink. Open Data > Edit Links and check the status column.
"This workbook contains one or more links that cannot be updated." A source file has been renamed, moved or deleted. Use ChangeLink to point at the real location, or use Edit Links > Change Source. If the source no longer exists, the only safe options are relinking to a replacement file or removing the link, which keeps the last cached values as stored numbers.
The macro stops on a permission or network error. The share may be offline or the user may lack read access. Test on a local copy to confirm the code is fine before blaming the link.
It used to work and now does not. Check for a recent move to OneDrive or SharePoint, a renamed folder, or a change in drive letter. Rebuild the path in ChangeLink and retest.
Frequently Asked Questions
Does UpdateLink open the source workbook?
It reads values from the source file and can open it in the background. Closed sources are read from their saved state, so unsaved edits in the source are not picked up.
What do the UpdateLinks numbers in Workbooks.Open mean?
0 means do not update, 1 updates external references, 2 updates remote references, and 3 updates all. Omitting the argument brings up the prompt.
Can I update links without a macro?
Yes. Data > Edit Links, then Update Values, refreshes all links by hand. The macro matters when the refresh has to happen repeatedly or unattended.
Why does LinkSources return nothing?
Because there are no links of that type, or because the references are data connections rather than workbook links. Check the Edit Links dialog to see which kind you have.
Will this work on Mac?
The main methods behave the same, but path formats differ and some Windows-only shell features are unavailable. Build paths with the correct slash direction for the platform.
Can I update links in a closed workbook?
Not directly. The link list and update methods apply to an open workbook, so open it first (with the desired UpdateLinks value), update, save and close.