+ Reply to Thread
Results 1 to 7 of 7

Thread: Change source of a link in VBA

  1. #1
    Registered User
    Join Date
    09-03-2010
    Location
    London
    MS-Off Ver
    Excel 2007
    Posts
    12

    Change source of a link in VBA

    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.

  2. #2
    Forum Guru Mordred's Avatar
    Join Date
    07-06-2010
    Location
    Winnipeg, Canada
    MS-Off Ver
    2007, 2010
    Posts
    2,276

    Re: Change source of a link in VBA

    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!

  3. #3
    Registered User
    Join Date
    09-03-2010
    Location
    London
    MS-Off Ver
    Excel 2007
    Posts
    12

    Re: Change source of a link in VBA

    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

  4. #4
    Valued Forum Contributor OnErrorGoto0's Avatar
    Join Date
    12-30-2011
    Location
    I DO NOT POST HERE ANYMORE
    MS-Off Ver
    I DO NOT POST HERE ANYMORE
    Posts
    1,647

    Re: Change source of a link in VBA

    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.

  5. #5
    Registered User
    Join Date
    09-03-2010
    Location
    London
    MS-Off Ver
    Excel 2007
    Posts
    12

    Re: Change source of a link in VBA

    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

  6. #6
    Valued Forum Contributor OnErrorGoto0's Avatar
    Join Date
    12-30-2011
    Location
    I DO NOT POST HERE ANYMORE
    MS-Off Ver
    I DO NOT POST HERE ANYMORE
    Posts
    1,647

    Re: Change source of a link in VBA

    Quote Originally Posted by DeeCeeL View Post
    Why do you include the on error resume next statement?
    Lazy way of handling an incorrect file path.
    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?
    You could change this
            For i = LBound(arrLinks) To UBound(arrLinks)
                ActiveWorkbook.ChangeLink arrLinks(i), "G:\Example\Budgets\example.xls", xlLinkTypeExcelLinks
            Next i
    to this
                ActiveWorkbook.ChangeLink arrLinks(1), "G:\Example\Budgets\example.xls", xlLinkTypeExcelLinks
    though I don't really see what you gain.
    Good luck.

  7. #7
    Registered User
    Join Date
    09-03-2010
    Location
    London
    MS-Off Ver
    Excel 2007
    Posts
    12

    Re: Change source of a link in VBA

    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

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.2.0