MrPatterns
MrPatterns

Reputation: 4434

How do I easily change hardcoded links to a file in Excel?

I have a project where I maintain a list of all my students and their information in an Excel file labeled "BigList.xlsx". Then, I have about 40-50 other separate ancillary excel files that link to BigList by using VLOOKUP.

For example, in cell A1 of an ancillary file you might see a formula that looks like this:

=Vlookup(B3, 
    'c:\documents and settings\user\desktop\[BigList.xlsx]Sheet1'!$a$1:$b$10000,
    2,false).

The vlookup link above references BigList.xlsx. However, I just realized that I need to change that file name to something else, like MasterDatabase.xlsm (notice the different extension). Is there an easy way to do this without having to manually go through all 40-50 files and doing a find & replace?

I think the basic idea is to change a hardcoded link into a dynamic one where I can change the filename of BigList.xlsx anytime, and not have to go back through all 40-50 files to update their links.

Upvotes: 2

Views: 7427

Answers (3)

brettdj
brettdj

Reputation: 55672

This code will automate the link change directly

  1. Update your paths to BigList.xlsx and MasterDatabase.xlsm in the code
  2. Update your path to the 40-50 files (I have used c:\temp\")
  3. The code will then open both these files (for quicker relinking), then one by open the files in strFilePath, change the link from WB1 (strOldMasterFile ) to Wb2 (strOldMasterFile ), then close the saved file

Please note it assumes all these files are closed on code start, as the code will open these file

    Sub ChangeLinks()
        Dim strFilePath As String
        Dim strFileName As String
        Dim strOldMasterFile As String
        Dim strNewMasterFile As String

        Dim WB1 As Workbook
        Dim WB2 As Workbook
        Dim WB3 As Workbook

        Dim lngCalc As Long    

        strOldMasterFile = "c:\testFolder\bigList.xlsx"
        strNewMasterFile = "c:\testFolder\newFile.xlsm"

        On Error Resume Next
        Set WB1 = Workbooks.Open(strOldMasterFile)
        Set WB2 = Workbooks.Open(strNewMasterFile)
        If WB1 Is Nothing Or WB2 Is Nothing Then
            MsgBox "One (or both) of " & vbnerwline & strOldMasterFile & vbNewLine & strNewMasterFile & vbNewLine & "cannot be found"
            WB1.Close False
            WB2.Close False
            Exit Sub
        End If
        On Error GoTo 0

        With Application
            .DisplayAlerts = False
            .ScreenUpdating = False
            .EnableEvents = False
            lngCalc = .Calculation
            .Calculation = xlCalculationManual 
        End With

        strFilePath = "c:\temp\"
        strFileName = Dir(strFilePath & "*.xls*")

        'Error handling as link may not exist in all files
        On Error Resume Next
        Do While Len(strFileName) > 0
            Set WB2 = Workbooks.Open(strFilePath & strFileName, False)
            WB2.ChangeLink strOldMasterFile, strNewMasterFile, xlExcelLinks
            WB2.Save
            WB2.Close False
            strFileName = Dir
        Loop
        On Error GoTo 0

        WB1.Close False
        WB2.Close False

        With Application
            .DisplayAlerts = True
            .ScreenUpdating = True
            .EnableEvents = True
            .Calculation = lngCalc
        End With

        End Sub

Upvotes: 2

mischab1
mischab1

Reputation: 1601

You can do this in Excel 2010 without using any code. (If memory serves, it will also work in earlier versions of Excel.)

  1. Open all 50 ancillary excel files in Excel at the same time.
  2. Open BigList.xlsx. (You now have 51 files open in Excel.)
  3. Click File - Save As and save BigList as MasterDatabase.xlsm
  4. Close the new MasterDatabase.xlsm file.
  5. Look at one of the ancillary files and verify that Excel has it pointed to the new file.
  6. Close and save all files.

Upvotes: 3

assylias
assylias

Reputation: 328568

This should do what you require - maybe not super fast but if you only need to do it once on 50 workbooks it should be good enough. Note that the replace line should make the replacement in all the sheets of the workbook.

Option Explicit

Public Sub replaceLinks()

    Dim path As String
    Dim file As String
    Dim w As Workbook
    Dim s As Worksheet

    On Error GoTo error_handler

    path = "C:\Users\xxxxxx\Documents\Test\"
    Application.DisplayAlerts = False
    Application.ScreenUpdating = False

    file = Dir$(path & "*.xlsx", vbNormal)
    Do Until LenB(file) = 0
        Set w = Workbooks.Open(path & file)
        ActiveSheet.Cells.Replace What:="'THE_LINK_YOU_WANT_TO_CHANGE'!", _
                Replacement:="'THE_NEW_LINK'!", LookAt:=xlPart
        w.Save
        w.Close
        file = Dir$
    Loop

    Application.DisplayAlerts = True
    Application.ScreenUpdating = True

    Exit Sub

error_handler:
    MsgBox Err.Description
    Application.DisplayAlerts = True
    Application.ScreenUpdating = True

End Sub

Upvotes: 4

Related Questions