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.

S:\OldPath\OldName1.xlsx

<tbody>
</tbody>
S:\NewPath\NewName1.xlsx

<tbody>
</tbody>
S:\OldPath\OldName2.xlsx

<tbody>
</tbody>
S:\NewPath\NewName2.xlsx

<tbody>
</tbody>
S:\OldPath\OldName3.xlsxS:\NewPath\NewName3.xlsx

<tbody>
</tbody>
 

Excel Facts

Test for Multiple Conditions in IF?
Use AND(test, test, test, test) or OR(test, test, test, ...) as the logical_test argument of IF.
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,214,951
Messages
6,122,449
Members
449,083
Latest member
Ava19

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