changing links workbook
RH

Forum home » Delegate support and help forum » Microsoft Excel VBA Training and help » Changing links in a workbook

Changing links in a workbook

resolvedResolved · Medium Priority · Version 2013

David has attended:
Excel Advanced course
Excel VBA Introduction course
Excel VBA Intermediate course
Excel VBA Advanced course

Changing links in a workbook

hi everyone,

I am looking for a macro that updates the 6 links in my workbook.

In Sheets("Start Here").Range("A6:A12") i have the file name of the new files I wish to update my links with AND the new files should be in the same folder as the current workbook. In Range("B6:B12") i have a key word that identifies each of the files. I want to be able to use the key word to identify which of the new files corresponds to the old file of the link it needs to update and update it and move through all 6 files. I am stuck.

I've tried to spec out what i think needs to happen

1. Get the current links
2. Search each link for the 6 key words
3. If the key word matches then find the path of the new file and replace
4. REPEAT

and this what i've come up with - the bit in italics is where its getting stuck - i can seem to get the cell reference that has the file name in to create the path of the new file:

[Code]
Sub ListLinks()

Dim wb As Workbook
Set wb = Application.ActiveWorkbook
Dim Path As String
Path = Application.ActiveWorkbook.Path
Dim rngX

For Each link In wb.LinkSources(xlExcelLinks)
If InStr(link, "Balanced") Then
Set rngX = Sheets("Start Here").Range("B6:B12").Find(What:="Balanced", LookIn:=xlValues, lookat:=xlPart)
wb.ChangeLink Name:=link, NewName:=Path&rngX.Address.Offset(-1, 0), Type:=xlExcelLinks
End If
Next link

End Sub

RE: Changing links in a workbook

Hi David,

Thank you for the forum question.

I hope my code below can get you in the right direction.


Sub ListLinks()

Dim wb As Workbook
Set wb = Application.ActiveWorkbook
Dim Path As String
Path = Application.ActiveWorkbook.Path
Dim link As Variant
Dim NewName As String



For Each link In wb.LinkSources(xlExcelLinks)
If InStr(link, "Balanced") Then
For Each cl In Range("b6:b12")
NewName = Path & "\" & cl.Offset(0, -1).Address
wb.ChangeLink Name:=link, NewName:=NewName, Type:=xlExcelLinks
Next cl
End If
Next link

End Sub


Kind regards

Jens Bonde
Microsoft Office Specialist Trainer

Tel: 0207 987 3777
STL - https://www.stl-training.co.uk
98%+ recommend us

London's leader with UK wide delivery in Microsoft Office training and management training to global brands, FTSE 100, SME's and the public sector

RE: Changing links in a workbook

hi Jens,

Thanks for your help but i don't quite understand what the cl is doing? We need to find the word Balanced in the range b6:b12 and then get the value in the corresponding a column (so if balanced is in b7 then i want the value in cell a7 - which is the name of the new file)

RE: Changing links in a workbook

Hi David,

Do you get what you want if you change the line in my code?

NewName = Path & "\" & cl.Offset(0, -1).Address

to

NewName = Path & "\" & cl.Offset(0, -1).value


Kind regards

Jens Bonde
Microsoft Office Specialist Trainer

Tel: 0207 987 3777
STL - https://www.stl-training.co.uk
98%+ recommend us

London's leader with UK wide delivery in Microsoft Office training and management training to global brands, FTSE 100, SME's and the public sector

RE: Changing links in a workbook

Yes i do :)

Can you explain what the For Each cl is doing?

RE: Changing links in a workbook

Hi David,

The Each in cl loop through the cells in range("b6:b12")

I can see that I have forgot something in my answer. I need to find only the word Balanced in the range B6:B12.

Please see amended code below:

For Each link In wb.LinkSources(xlExcelLinks)
If InStr(link, "Balanced") Then
For Each cl In Range("b6:b12")
if cl.value="Balanced" Then
NewName = Path & "\" & cl.Offset(0, -1).Address
wb.ChangeLink Name:=link, NewName:=NewName, Type:=xlExcelLinks
end if
Next cl
End If
Next link

End Sub

The For Each cl is looping through the range(

Kind regards

Jens Bonde
Microsoft Office Specialist Trainer

Tel: 0207 987 3777
STL - https://www.stl-training.co.uk
98%+ recommend us

London's leader with UK wide delivery in Microsoft Office training and management training to global brands, FTSE 100, SME's and the public sector

RE: Changing links in a workbook

Jens,

Thanks. Its a little more complicated I was just using "Balanced" as a test. I actually have a range of keywords so i tried to modify the code but it doesn't seem to work


Dim wb As Workbook
Set wb = Application.ActiveWorkbook
Dim Path As String
Path = Application.ActiveWorkbook.Path
Dim NewName As String
Dim KeyWord As Variant

KeyWord = Array("Cautious", "Balanced", "Growth", "Global", "UK", "Bond")

For Each link In wb.LinkSources(xlExcelLinks)
If InStr(link, KeyWord) Then
For Each cl In Sheets("Start Here").Range("b6:b12")
If cl.Value = KeyWord Then
NewName = Path & "\" & cl.Offset(0, -1).Value
wb.ChangeLink Name:=link, NewName:=NewName, Type:=xlExcelLinks
End If
Next KeyWord
Next cl
End If
Next link

End Sub

RE: Changing links in a workbook

Hi David,

Test below.

I cannot test it but I hope it is working.

Dim wb As Workbook
Set wb = Application.ActiveWorkbook
Dim Path As String
Path = Application.ActiveWorkbook.Path
Dim NewName As String
Dim KeyWord As Variant

KeyWord = Array("Cautious", "Balanced", "Growth", "Global", "UK", "Bond")

For Each link In wb.LinkSources(xlExcelLinks)

If InStr(link, IsInArray(link.Value, KeyWord)) Then
For Each cl In Sheets("Start Here").Range("b6:b12")
If IsInArray(cl.Value, KeyWord) Then
NewName = Path & "\" & cl.Offset(0, -1).Value
wb.ChangeLink Name:=link, NewName:=NewName, Type:=xlExcelLinks
End If
Next KeyWord
Next cl
End If
Next link

End Sub

Function IsInArray(stringToBeFound As String, arr As Variant)
IsInArray = Not IsError(Application.Match(stringToBeFound, arr, 0))


End Function


Kind regards

Jens Bonde
Microsoft Office Specialist Trainer

Tel: 0207 987 3777
STL - https://www.stl-training.co.uk
98%+ recommend us

London's leader with UK wide delivery in Microsoft Office training and management training to global brands, FTSE 100, SME's and the public sector

RE: Changing links in a workbook

I get a compile error: Invalid Next control variable reference

RE: Changing links in a workbook

Hi David,

Try below:


Dim wb As Workbook
Set wb = Application.ActiveWorkbook
Dim Path As String
Path = Application.ActiveWorkbook.Path
Dim NewName As String
Dim KeyWord As Variant

KeyWord = Array("Cautious", "Balanced", "Growth", "Global", "UK", "Bond")

For Each link In wb.LinkSources(xlExcelLinks)

If InStr(link, IsInArray(link.Value, KeyWord)) Then
For Each cl In Sheets("Start Here").Range("b6:b12")
If IsInArray(cl.Value, KeyWord) Then
NewName = Path & "\" & cl.Offset(0, -1).Value
wb.ChangeLink Name:=link, NewName:=NewName, Type:=xlExcelLinks
End If

Next cl
End If
Next link

End Sub

Function IsInArray(stringToBeFound As String, arr As Variant)
IsInArray = Not IsError(Application.Match(stringToBeFound, arr, 0))


End Function

Kind regards

Jens Bonde
Microsoft Office Specialist Trainer

Tel: 0207 987 3777
STL - https://www.stl-training.co.uk
98%+ recommend us

London's leader with UK wide delivery in Microsoft Office training and management training to global brands, FTSE 100, SME's and the public sector

RE: Changing links in a workbook

Nope - just a run-time error '424'

RE: Changing links in a workbook


Hi David,

Which line return the error?


Kind regards

Jens Bonde
Microsoft Office Specialist Trainer

Tel: 0207 987 3777
STL - https://www.stl-training.co.uk
98%+ recommend us

London's leader with UK wide delivery in Microsoft Office training and management training to global brands, FTSE 100, SME's and the public sector

RE: Changing links in a workbook

If InStr(link, IsInArray(link.Value, KeyWord)) Then

This line

RE: Changing links in a workbook

hi Jens.

Thanks for all your help, i've managed to solve it:

Sub ChangeLinks()

Dim wb As Workbook
Set wb = Application.ActiveWorkbook
Dim Path As String
Path = Application.ActiveWorkbook.Path
Dim NewName As String
Dim KeyWord As Variant


For Each link In wb.LinkSources(xlExcelLinks)
For Each KeyWord In Array("Cautious", "Balanced", "Growth", "Global", "UK", "Bond")
If InStr(link, KeyWord) Then
For Each cl In Sheets("Start Here").Range("b6:b14")
If cl.Value = KeyWord Then
NewName = Path & "\" & cl.Offset(0, -1).Value
wb.ChangeLink Name:=link, NewName:=NewName, Type:=xlExcelLinks
End If
Next cl
End If
Next KeyWord
Next link

End Sub

RE: Changing links in a workbook

Hi David,

Brilliant well done.

I have been busy today running a course, so I have not had the time for the code.

And thank you for sharing the code. I have never tried the InStr inside the IsInArray before so well done finding a working solution.


Kind regards

Jens Bonde
Microsoft Office Specialist Trainer

Tel: 0207 987 3777
STL - https://www.stl-training.co.uk
98%+ recommend us

London's leader with UK wide delivery in Microsoft Office training and management training to global brands, FTSE 100, SME's and the public sector

 

Training courses

 

Training information:

Welcome. Please choose your application (eg. Excel) and then post your question.

Our Microsoft Qualified trainers will then respond within 24 hours (working days).

Frequently Asked Questions
What does 'Resolved' mean?

Any suggestions, questions or comments? Please post in the Improve the forum thread.


 

Excel tip:

Generate randon numbers

Some types of analysis require you to use randomly generated numbers. You can also use randomly generated numbers to quickly populate an Excel spreadsheet. There's an easy function you can use to do this automatically. Here are a few of the ways you can use it:

Type =RAND() in a cell to generate a number between 0 and 1.
Type =RAND()*100 to generate a number between 1 and 100.

View all Excel hints and tips


Server loaded in 0.05 secs.