Macro to change links to the current workbook

mahon

New Member
Joined
Jul 12, 2013
Messages
3
I have a macro that imports sheets from another workbook, then calls the below code to change any links on the imported sheets to the sheet of the same name in the active workbook. All sheets have the same names between the two books, with the exception of one which was changed between versions of the file at some point erroneously.

My issue is that when the code comes to a link to this differently named sheet, it stops and does not continue through the rest of the links. Is there a way I can have it break those links and continue on, or just delete the value in those cells?

Code:
Sub MyChangeLinkSource()


    Dim varFileName As Variant
    Dim arrLinks    As Variant
    Dim ws          As Worksheet
    Dim MyLink      As String
    Dim i           As Long


    'Object references
    Set ws = ActiveSheet


    'Check for links
    arrLinks = ActiveWorkbook.LinkSources(xlExcelLinks)
    If IsEmpty(arrLinks) Then
        Exit Sub
    End If


    'Get new source file
    varFileName = ActiveWorkbook.Name
    If varFileName = "False" Then Exit Sub
    
    'Error handler to re-protect workbook in case of errors
    On Error GoTo Handler


    ' Add braces [] around file name
    MyLink = Mid(varFileName, InStrRev(varFileName, "\") + 1)
    varFileName = Replace(varFileName, MyLink, "[" & MyLink & "]")


    'Change link source
    arrLinks = ActiveWorkbook.LinkSources(xlExcelLinks)
    
    For i = 1 To UBound(arrLinks)
        ' Add braces [] around each link filename
        MyLink = Mid(arrLinks(i), InStrRev(arrLinks(i), "\") + 1)
        MyLink = Replace(arrLinks(i), MyLink, "[" & MyLink & "]")
        
        ' Replace old LinkSources file name with new
        If Not ws.Cells.Find(MyLink, , xlFormulas) Is Nothing Then
            ws.Cells.Replace What:=MyLink, Replacement:=varFileName, LookAt:=xlPart
        End If
        
    Next i


    'Handle errors
Handler:
    If Err.Number <> 0 Then
        MsgBox "Error " & Err.Number & vbCrLf & Err.Description & _
                vbLf & vbLf & "There was an error linking prior period tabs to current workbook.  Please click ok and review the new tabs."
    End If
    
End Sub

Much of this code was made possible with help from this board. If in the meantime I work out a solution I'll be sure to post an update.

Thanks!
 

Excel Facts

What is the fastest way to copy a formula?
If A2:A50000 contain data. Enter a formula in B2. Select B2. Double-click the Fill Handle and Excel will shoot the formula down to B50000.
Ok, I solved my problem by adding a line in the initial macro to rename the tab that is being linked to before the import, but this is not as clean as I would like and I feel like the issue will crop up again. If none of this made any sense, don't waste your time as my problem is solved for now. If anyone has any ideas to improve on this fix though please share!
 
Upvote 0

Forum statistics

Threads
1,214,982
Messages
6,122,573
Members
449,089
Latest member
Motoracer88

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