+ Reply to Thread
Results 1 to 7 of 7

Insert variables from two cells in a formula

  1. #1
    Registered User
    Join Date
    09-28-2010
    Location
    Latvia
    MS-Off Ver
    Excel 2007/2010
    Posts
    18

    Post Insert variables from two cells in a formula

    I have a formula to read data from another excel workbook.

    The formula looks like this:
    ='D:\Data\Employees\[Name Surname_performance.xlsx]Overview'!$F$3
    (e.g. ='D:\Data\Employees\[John Smith_performance.xlsx]Overview'!$F$3)

    and filenames look like this:
    Name Surname_performance.xlsx
    (e.g. John Smith_performance.xlsx)

    I have two more columns, one for names and the other for surnames, column B and column C respectively.
    I need the formula do be something like this:

    ='D:\Data\Employees\["B2" "C2"_performance.xlsx]Overview'!$F$3

    (B2=John, C2=Smith)
    I also need a space between Name and Surname.

    Will apprecciate any help.
    Last edited by evilgrin; 09-30-2010 at 03:22 PM.

  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: Insert variables from two cells in a formula

    The function you're looking for is called INDIRECT(). That function allows you to piece together pieces of a text string to create a reference that is then evaluated as normal text string in a formula.

    The function in your formula would look like this:

    =INDIRECT("'D:\Data\Employees\[" & B2 & " " & C2 & _performance.xlsx]Overview'!$F$3")


    The main problem with using this function is that the resulting formula will only pull data from external workbooks if the workbooks are open. It will pull data from within the existing workbook just fine...I suppose because it, too, is open.

    The only way I know of to dynamically change a formula in a cell to utilize other cells as partial text strings to create an external workbook reference is to use VBA. A macro would actually rewrite the formula in the cell/column in question so that it is a normal formula when it is done, no variables in that actual cell.
    _________________
    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
    09-28-2010
    Location
    Latvia
    MS-Off Ver
    Excel 2007/2010
    Posts
    18

    Re: Insert variables from two cells in a formula

    I tried the formula but Excel gave me an error. It seems that it was unable to understand the whole filename of the referenced workbook. It highlighted _performance.xlsx]Overview'
    as the error.

    I do need it to pull data from closed workbooks (opening 80+ workbooks would take a lot of time) so a macro might be the best solution. But I'm not very familiar with macros and have no idea how to write one for this

    I figured that I could copy ='D:\Data\Employees\[Name Surname_performance.xlsx]Overview'!$F$3 to all of the cells and the macro would need to find all cells containing 'Name Surname' in the specified range (it's not just one columns, there are several columns - thus the range, also the last numbers change) and replace 'Name Surname' with the corresponding values in B and C columns.
    Last edited by evilgrin; 09-30-2010 at 12:45 PM.

  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: Insert variables from two cells in a formula

    Something like this:
    Please Login or Register  to view this content.

  5. #5
    Registered User
    Join Date
    09-28-2010
    Location
    Latvia
    MS-Off Ver
    Excel 2007/2010
    Posts
    18

    Re: Insert variables from two cells in a formula

    Thanks, works perfect except for two things:

    1. I need these formulas in a number of columns
    2. The numbers at the end of formulas increase in each next column.

    Here's a sample worksheet.
    Attached Files Attached Files
    Last edited by evilgrin; 09-30-2010 at 01:59 PM.

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

    Re: Insert variables from two cells in a formula

    Just slip an additional loop inside the main one:
    Please Login or Register  to view this content.

  7. #7
    Registered User
    Join Date
    09-28-2010
    Location
    Latvia
    MS-Off Ver
    Excel 2007/2010
    Posts
    18

    Re: Insert variables from two cells in a formula

    Thank you very much!

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