Hi,
As part of a VBA sequence, I want to change the source of a link in an excel spreadsheet.
In the file there is only one source. I was the macro to change whatever source link is in the file to a given link.
I started like that :
Sub Changelink()
Dim x0 As Variant
x0 = ActiveWorkbook.LinkSources(xlExcelLinks)
ChDir "G:\Example\Budgets"
ActiveWorkbook.ChangeLink Name:=x0, _
NewName:="example.xls", Type:=xlExcelLinks
End Sub
Many thanks for your help.
Dee
Last edited by DeeCeeL; 02-14-2012 at 10:39 AM.
Hi DeeCeeL.
Your post does not comply with Rule 3 of our Forum RULES. Use code tags around code. Posting code without them makes your code hard to read and difficult to be copied for testing. Highlight your code and click the # at the top of your post window. For more information about these and other tags, found here
Please leave a message after the beep!
My apologies, here you go.
Sub Changelink() Dim x0 As Variant x0 = ActiveWorkbook.LinkSources(xlExcelLinks) ChDir "G:\Example\Budgets" ActiveWorkbook.ChangeLink Name:=x0, _ NewName:="example.xls", Type:=xlExcelLinks End Sub
It would be something like
Sub ChangeLinks() Dim arrLinks Dim i As Long arrLinks = ActiveWorkbook.LinkSources(xlExcelLinks) If Not IsEmpty(arrLinks) Then On Error Resume Next Application.DisplayAlerts = False For i = LBound(arrLinks) To UBound(arrLinks) ActiveWorkbook.ChangeLink arrLinks(i), "G:\Example\Budgets\example.xls", xlLinkTypeExcelLinks Next i Application.DisplayAlerts = True End If End Sub
Good luck.
Thank you Rori.
Why do you include the on error resume next statement?
Your code covers an array, would there be a simpler way to perform this task, knowing that there will be only one source link in the spreadsheet?
Many thanks in advance.
Dee
Lazy way of handling an incorrect file path.
You could change thisYour code covers an array, would there be a simpler way to perform this task, knowing that there will be only one source link in the spreadsheet?
to thisFor i = LBound(arrLinks) To UBound(arrLinks) ActiveWorkbook.ChangeLink arrLinks(i), "G:\Example\Budgets\example.xls", xlLinkTypeExcelLinks Next i
though I don't really see what you gain.ActiveWorkbook.ChangeLink arrLinks(1), "G:\Example\Budgets\example.xls", xlLinkTypeExcelLinks
Good luck.
Thank you Rori. You are correct, it would not add anything, but it helps me understand the code and the case an awful lot.
Many thanks again.
Best
D
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks