VBA Batch Update external links

JasonExcel

New Member
Joined
Mar 9, 2018
Messages
6
I have an Excel file that is looking at a few hundred external Excel files. These external links now need to be changed to new files each of which has a new file path and name. I have a list of all the old files paths and names in column A of a spreadsheet and a list of all the new files paths and names in column B of the same sheet. Is there a way to loop through the spreadsheet and replace all the old external links per column A with their equivalents from the same row in column B.

[TABLE="width: 500"]
<tbody>[TR]
[TD][TABLE="width: 177"]
<tbody>[TR]
[TD="width: 177"]S:\OldPath\OldName1.xlsx[/TD]
[/TR]
</tbody>[/TABLE]
[/TD]
[TD][TABLE="width: 64"]
<tbody>[TR]
[TD="width: 64"]S:\NewPath\NewName1.xlsx[/TD]
[/TR]
</tbody>[/TABLE]
[/TD]
[/TR]
[TR]
[TD][TABLE="width: 177"]
<tbody>[TR]
[TD="width: 177"]S:\OldPath\OldName2.xlsx[/TD]
[/TR]
</tbody>[/TABLE]
[/TD]
[TD][TABLE="width: 64"]
<tbody>[TR]
[TD="width: 64"]S:\NewPath\NewName2.xlsx[/TD]
[/TR]
</tbody>[/TABLE]
[/TD]
[/TR]
[TR]
[TD]S:\OldPath\OldName3.xlsx[/TD]
[TD]S:\NewPath\NewName3.xlsx[/TD]
[/TR]
</tbody>[/TABLE]
 

Excel Facts

Enter current date or time
Ctrl+: enters current time. Ctrl+; enters current date. Use Ctrl+: Ctrl+; Enter for current date & time.
The following macro assumes that the active sheet contains the data, and it also assumes that the new link is a valid one...

Code:
Option Explicit

Sub ChangeLinks()

    Dim currentLink As Variant
    Dim newLink As Variant
    Dim lookupRange As Range
    Dim lastRow As Long
    Dim i As Long
    
    lastRow = Cells(Rows.Count, "A").End(xlUp).Row
    
    Set lookupRange = Range("A1:B" & lastRow)

    For Each currentLink In ActiveWorkbook.LinkSources(Type:=xlExcelLinks)
        newLink = Application.VLookup(currentLink, lookupRange, 2, 0)
        If Not IsError(newLink) Then
            ActiveWorkbook.ChangeLink _
                Name:=currentLink, _
                NewName:=newLink, _
                Type:=xlLinkTypeExcelLinks
        End If
    Next currentLink
    
End Sub

Hope this helps!
 
Upvote 0
The following macro assumes that the active sheet contains the data, and it also assumes that the new link is a valid one...

Code:
Option Explicit

Sub ChangeLinks()

    Dim currentLink As Variant
    Dim newLink As Variant
    Dim lookupRange As Range
    Dim lastRow As Long
    Dim i As Long
    
    lastRow = Cells(Rows.Count, "A").End(xlUp).Row
    
    Set lookupRange = Range("A1:B" & lastRow)

    For Each currentLink In ActiveWorkbook.LinkSources(Type:=xlExcelLinks)
        newLink = Application.VLookup(currentLink, lookupRange, 2, 0)
        If Not IsError(newLink) Then
            ActiveWorkbook.ChangeLink _
                Name:=currentLink, _
                NewName:=newLink, _
                Type:=xlLinkTypeExcelLinks
        End If
    Next currentLink
    
End Sub

Hope this helps!

Thank you Domenic! This solution worked perfectly, I didn't even adjust a single line of code. You have saved me an enormous amount of monotony and repetition. Thank you very very much! :):):):)
 
Upvote 0

Forum statistics

Threads
1,224,748
Messages
6,180,721
Members
452,995
Latest member
isldboy

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top