+ Reply to Thread
Results 1 to 11 of 11

VBA - Change linked file

  1. #1
    Registered User
    Join Date
    08-09-2013
    Location
    Germiston, South Africa
    MS-Off Ver
    Excel 2010
    Posts
    12

    VBA - Change linked file

    I require the VBA code for the user to choose and then change a linked file

  2. #2
    Forum Moderator - RIP Richard Buttrey's Avatar
    Join Date
    01-14-2008
    Location
    Stockton Heath, Cheshire, UK
    MS-Off Ver
    Office 365, Excel for Windows 2010 & Excel for Mac
    Posts
    29,464

    Re: VBA - Change linked file

    Hi, and welcome to the forum.

    Try

    Please Login or Register  to view this content.
    Last edited by Richard Buttrey; 08-10-2013 at 01:52 PM.
    Richard Buttrey

    RIP - d. 06/10/2022

    If any of the responses have helped then please consider rating them by clicking the small star icon below the post.

  3. #3
    Registered User
    Join Date
    08-09-2013
    Location
    Germiston, South Africa
    MS-Off Ver
    Excel 2010
    Posts
    12

    Re: VBA - Change linked file

    Thanks Richard but not what I require
    I am teaching myself VBA and am perhaps not explaining myself clearly

    I have a workbook "A" which is linked to Workbook "B"
    The user is working in workbook "A" and the code already written requires them to change the link from workbook "B" to one chosen from the same dir

  4. #4
    Forum Moderator - RIP Richard Buttrey's Avatar
    Join Date
    01-14-2008
    Location
    Stockton Heath, Cheshire, UK
    MS-Off Ver
    Office 365, Excel for Windows 2010 & Excel for Mac
    Posts
    29,464

    Re: VBA - Change linked file

    Hi,

    In that case, untested but try this slight modification


    Please Login or Register  to view this content.

  5. #5
    Registered User
    Join Date
    08-09-2013
    Location
    Germiston, South Africa
    MS-Off Ver
    Excel 2010
    Posts
    12

    Re: VBA - Change linked file

    This makes more sense to me but does not work fully
    I can see how the code will get the "NewName"
    How does the code get linked "CurrentFileName"?
    Also does the "NewName" file have to be opened?

  6. #6
    Registered User
    Join Date
    08-09-2013
    Location
    Germiston, South Africa
    MS-Off Ver
    Excel 2010
    Posts
    12

    Re: VBA - Change linked file

    I have tried this - the new "NewName" seems work as I have to choose and open
    After that the code breaks down - I think it is the "stCurrentFName=ActiveWorkBook.Name" as this should actually refer to the "linked file" and not the "current workbook"

  7. #7
    Forum Moderator - RIP Richard Buttrey's Avatar
    Join Date
    01-14-2008
    Location
    Stockton Heath, Cheshire, UK
    MS-Off Ver
    Office 365, Excel for Windows 2010 & Excel for Mac
    Posts
    29,464

    Re: VBA - Change linked file

    Sorry, I did say it was untested!

    Try instead

    Please Login or Register  to view this content.
    ...assuming there is only one link in operation.

    No, the NewName File does not need to be opened. The instruction GetOpenFileName does as it says on the tin. When the user points to it in the usual windows file dialog box it's assigned to the variable when you click on the 'Open' buttton - which I agree is confusing since it doesn't actually open the file. I suspect MS were just being lazy and used the same FileOpen dialog box as you would use if you do want to open a file.

  8. #8
    Registered User
    Join Date
    08-09-2013
    Location
    Germiston, South Africa
    MS-Off Ver
    Excel 2010
    Posts
    12

    Re: VBA - Change linked file

    Still not working
    Does the "stCurrentFName" for the link file not also give the path which would then change the last line of code?
    I am going to try this again

  9. #9
    Registered User
    Join Date
    08-09-2013
    Location
    Germiston, South Africa
    MS-Off Ver
    Excel 2010
    Posts
    12

    Re: VBA - Change linked file

    Hi Richard
    I have taken the "Path" out of the code and it now works just as I want it to
    Here it is for your (or other users) records

    Dim stCurrentFname As String, stNewFName As String, stPathName As String
    stCurrentFname = ThisWorkbook.LinkSources(XlLink.xlExcelLinks)(1)
    stNewFName = Application.GetOpenFilename
    ActiveWorkbook.ChangeLink Name:=stCurrentFname, NewName:=stNewFName

    This code changes the "Linked File" to the "New User Chosen File"

    You are correct that the code only picks the first "Linked File" - This is fine for me as there is only one Linked File at any one time
    However I tried it with two "Linked Files" and when I changed the (1) in "stCurrentFName" to (2) the code picks up the second file

    Thanks a lot
    You have helped solve the problem I have been battling over for a few weeks now

  10. #10
    Registered User
    Join Date
    08-09-2013
    Location
    Germiston, South Africa
    MS-Off Ver
    Excel 2010
    Posts
    12

    Re: VBA - Change linked file

    Hi Richard
    Me again - I am also new to ExcelForum - how do I rate the service I have received?
    Thanks again
    Hadj

  11. #11
    Forum Moderator - RIP Richard Buttrey's Avatar
    Join Date
    01-14-2008
    Location
    Stockton Heath, Cheshire, UK
    MS-Off Ver
    Office 365, Excel for Windows 2010 & Excel for Mac
    Posts
    29,464

    Re: VBA - Change linked file

    Quote Originally Posted by Hadj View Post
    Hi Richard
    Me again - I am also new to ExcelForum - how do I rate the service I have received?
    Thanks again
    Hadj
    I guess you found it. And thanks for the rep points.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. [SOLVED] sum(offset(... to linked file requires linked file to be open?
    By Oppressed1 in forum Excel General
    Replies: 5
    Last Post: 10-22-2012, 02:21 PM
  2. how to auto change the file name of a linked cell
    By twaddel in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 08-10-2012, 12:07 AM
  3. Replies: 0
    Last Post: 08-10-2011, 08:20 PM
  4. Two Linked Files, What If File Names Change Each Job
    By barrha0b in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 02-24-2010, 05:24 PM
  5. Replies: 2
    Last Post: 11-12-2009, 10:31 AM

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.6.0 RC 1