+ Reply to Thread
Results 1 to 4 of 4

VBA in Excel: define file to open (I retrieve data from 2 Excel files)

  1. #1
    Registered User
    Join Date
    02-13-2012
    Location
    NYC
    MS-Off Ver
    Excel 2003
    Posts
    39

    VBA in Excel: define file to open (I retrieve data from 2 Excel files)

    Hello,

    I have 2 Excel files open, file (A) is a data source file and file (B) is a file with various sheets being created with VBA.
    My VBA needs to be clear which of both files it refers to when reading data, and I believe that is my problem.

    I have the following written:


    Please Login or Register  to view this content.

    How should I correct my VBA so that it opens the specified file? And if the file is already open, to select it?
    Thank you for your help.
    Last edited by Cutter; 08-14-2012 at 07:26 PM. Reason: Added code tags

  2. #2
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: VBA in Excel: define file to open (I retrieve data from 2 Excel files)

    If there is a named range in your workbook called LastFileEdited, then you can set the variable this way as you open the file:

    Please Login or Register  to view this content.
    Be warned, this will only work if the string in that cell is the full path and name of a file.
    _________________
    Microsoft MVP 2010 - Excel
    Visit: Jerry Beaucaire's Excel Files & Macros

    If you've been given good help, use the icon below to give reputation feedback, it is appreciated.
    Always put your code between code tags. [CODE] your code here [/CODE]

    ?None of us is as good as all of us? - Ray Kroc
    ?Actually, I *am* a rocket scientist.? - JB (little ones count!)

  3. #3
    Registered User
    Join Date
    02-13-2012
    Location
    NYC
    MS-Off Ver
    Excel 2003
    Posts
    39

    Re: VBA in Excel: define file to open (I retrieve data from 2 Excel files)

    Thank you for your proposition JBeaucaire.

    The way I'm trying to make reference this particular Excel file easier is by defining it with 'W2bk' name.

    Long naming (that I prefer to avoid):

    Please Login or Register  to view this content.
    To avoid this long naming, I'm trying to call the file 'W2bk' so that I can reference it in the VBA code.

    When writing your suggestion, I still receive error message 13

    Please Login or Register  to view this content.
    Thank you for your help.

  4. #4
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: VBA in Excel: define file to open (I retrieve data from 2 Excel files)

    I showed you in my post how to do it all at once, set the variable and open the workbook simultaneously.

    You're receiving errors because either:

    1) A mismatch in the data variable types. (are all thress of those variables you're trying to use declared as STRING?)
    or
    2) The strings in those variables do not properly resolve to an existing file.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Tags for this Thread

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