+ Reply to Thread
Results 1 to 11 of 11

Return range name from concatenated strings

  1. #1
    Registered User
    Join Date
    03-29-2010
    Location
    Earth
    MS-Off Ver
    Excel 2007
    Posts
    37

    Return range name from concatenated strings

    I have financial data of various accounts stored in Workbook A.

    Workbook B defines a number of ranges as follows to access the data stored in Workbook A:

    dPath = path to Workbook A

    dFN1 = filename of Workbook A and worksheet name of sheet where Company 1's data is stored

    dAc5000 = range of data for account 5000 (eg "$F$6:$F$37")
    dAc5010 = range of data for account 5000 (eg "$G$6:$G$37")
    etc

    dDate = range of dates corresponding to the account data

    Workbook B sets out the account data in rows by looking up a date in row 1 and matching it to a corresponding date in a named range in Workbook A. The following formula is an example:

    =IF(ISNA(INDEX(INDIRECT(dPath&dFn1&dAc5000),MATCH(TEXT(G$1,"dd/mm/yyyy"),INDIRECT(dPath&dFn1&dDate),0))),0,INDEX(INDIRECT(dPath&dFn1&dAc5000),MATCH(TEXT(G$1,"dd/mm/yyyy"),INDIRECT(dPath&dFn1&dDate),0)))

    Using this kind of formula, I get the desired result, but the different account range names must be hard-coded into the formulae on each row.

    Instead of hard-coding the name dAc5000 into the above formula, I would like to have a generic formula that can be included on each row, and the data retrieved from Workbook A for a particular row dependant on the contents of a separate column that includes an account number (called, say, AcNo). This way I could change the layout/order of the rows simply by changing the account numbers in the AcNo column, without having to alter the formulae in each row.

    I have tried concatenating the string "dAc" to the contents of column AcNo, and using the returned string as an alternative range name in the above formula. For example, for row 5000, the string returned is dAc5000 (the same string as the named range already defined), so I adjusted the INDIRECT portion of the above formula to:

    INDIRECT(dPath&dFn1&"dAc"&AcNo)

    Unfortunately this results in a #REF! error, I think because the concatenation returns a string and not the corresponding named range. (Note, the external worksheet is open, and so the #REF! error is not because the external worksheet is closed.)

    Thanks in advance for any help.
    Last edited by cad1llac; 03-30-2010 at 01:56 AM.

  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: Return range name from concatenated strings

    Shot in the dark here...maybe:

    =INDIRECT(dPath&dFn1&INDIRECT("dAc"&AcNo))
    _________________
    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
    03-29-2010
    Location
    Earth
    MS-Off Ver
    Excel 2007
    Posts
    37

    Re: Return range name from concatenated strings

    Thanks JB...

    But have tried that already (see below).

    The only thing that has worked for me so far is to hard-code the account number into the formula:

    =INDIRECT(dPath&dFn1&dAc5000)

    I have also tried the following without getting the desired result:

    =INDIRECT(dPath&dFn1&B1)
    =INDIRECT(dPath&dFn1&"dAc"&A1))
    =INDIRECT(dPath&dFn1&INDIRECT("dAc"&A1))

    where A1:=5000 (original account number) and B1:="dAc"&A1 (concatenated string)

    In each case, B1 returns dAc5000 as a string only (as expected). Like you suggested, I had thought that perhaps a nested INDIRECT formula might then interpret the string as a defined name but it still returns a #REF! error.

    It's got me stumped...

  4. #4
    Forum Guru romperstomper's Avatar
    Join Date
    11-04-2008
    Location
    A1
    MS-Off Ver
    Most
    Posts
    12,302

    Re: Return range name from concatenated strings

    INDIRECT won't work unless it refers to an actual range, which it does not do here. You could use a table on a sheet with the references entered into cells and the names pointing at those cells.
    I imagine performance will be pretty slow with all those INDIRECT formulas in your workbook though.
    Remember what the dormouse said
    Feed your head

  5. #5
    Registered User
    Join Date
    03-29-2010
    Location
    Earth
    MS-Off Ver
    Excel 2007
    Posts
    37

    Re: Return range name from concatenated strings

    Thanks RS.


    Quote Originally Posted by romperstomper View Post
    INDIRECT won't work unless it refers to an actual range, which it does not do here.
    That seems to be the essence of this problem. I works when I use a combination of predefined names, but not when I want to use 2 strings combined to represent one of those names.

    Is there a way to have Excel recognize a concatenated string as a (predefined) name?


    Quote Originally Posted by romperstomper View Post
    You could use a table on a sheet with the references entered into cells and the names pointing at those cells.
    Assuming I have, say, a couple of hundred accounts of data in the external worksheet, I already have a couple of hundred corresponding named ranges pointing to a different column range for each account. Multiply this by a couple of hundred companies, and you can see that the naming exercise can quickly get out of control.

    So I read about and used the INDIRECT function, and arrived at a solution of defining a name for the data file path (in case the path changes), separate names for the file names/worksheets of different companies (so the user can input a code for each company of interest and data is retrieved based on the user input) and separate names for each account column range.

    I am now just trying to find the final piece of the puzzle - a way to retrieve account data based on the number of an account inputted into a column on the same row where the data is displayed.

    Would your idea of creating additional references to a separate table involving creating an additional name for each account? Is that what you had in mind? Can you elaborate? Would this solve the original problem of having to hardcode account numbers in the formulae on the main worksheet where the data is displayed?


    Quote Originally Posted by romperstomper View Post
    I imagine performance will be pretty slow with all those INDIRECT formulas in your workbook though.
    As mentioned, I already have one workable way of accessing the data (only by hardcoding the account numbers), with a worksheet with hundreds of formulae but very little noticeable performance decline.

    So I just expect this concept to provide some added flexibility without performance issues.

    If I had a workable solution without hardcoding the account numbers, I could try it out and see what performance issues, if any, it does bring up. A slight performance decline with added flexibility might be a better problem to have.

  6. #6
    Forum Guru romperstomper's Avatar
    Join Date
    11-04-2008
    Location
    A1
    MS-Off Ver
    Most
    Posts
    12,302

    Re: Return range name from concatenated strings

    Quote Originally Posted by cad1llac View Post
    Is there a way to have Excel recognize a concatenated string as a (predefined) name?
    Yes, but only if that name actually points to a range. Here, that is not the case - it's a string.
    Would your idea of creating additional references to a separate table involving creating an additional name for each account? Is that what you had in mind? Can you elaborate? Would this solve the original problem of having to hardcode account numbers in the formulae on the main worksheet where the data is displayed?
    If you create a table of addresses with the company name in one column and the range address in the next, you can probably do away with most of your names and use a lookup formula to return the correct range address to concatenate into your INDIRECT string.

    Does that make sense?

  7. #7
    Registered User
    Join Date
    03-29-2010
    Location
    Earth
    MS-Off Ver
    Excel 2007
    Posts
    37

    Thumbs up Re: Return range name from concatenated strings

    Thanks again, RS.

    Yes it does make sense, on both points.


    Quote Originally Posted by romperstomper View Post
    Yes, but only if that name actually points to a range. Here, that is not the case - it's a string.
    The solution I thought may be possible/available would be use (or create) a function that can convert the string in such a way that it is then recognized as the named reference and returns the actual reference of the name (in this case, the range).

    This could then be passed to the INDIRECT function as a range, and not a string.

    At least that was the theory...


    Quote Originally Posted by romperstomper View Post
    If you create a table of addresses with the company name in one column and the range address in the next, you can probably do away with most of your names and use a lookup formula to return the correct range address to concatenate into your INDIRECT string.
    I will try setting up the table of addresses. It may even end up being a little easier to maintain if the external data ranges change, rather than having to redefine names.

    I will post any feedback once I've had a chance to get it done.

    Thanks again for your time and useful feedback.

  8. #8
    Forum Guru romperstomper's Avatar
    Join Date
    11-04-2008
    Location
    A1
    MS-Off Ver
    Most
    Posts
    12,302

    Re: Return range name from concatenated strings

    Quote Originally Posted by cad1llac View Post
    The solution I thought may be possible/available would be use (or create) a function that can convert the string in such a way that it is then recognized as the named reference and returns the actual reference of the name (in this case, the range).

    This could then be passed to the INDIRECT function as a range, and not a string.
    All you would need to do is alter your dAc5000 name to point to a cell containing the text $B$1:$B$1000 for example. Then you could use INDIRECT("dAc"&A1) where A1 contains 5000.

  9. #9
    Registered User
    Join Date
    03-29-2010
    Location
    Earth
    MS-Off Ver
    Excel 2007
    Posts
    37

    Re: Return range name from concatenated strings

    Quote Originally Posted by romperstomper View Post
    All you would need to do is alter your dAc5000 name to point to a cell containing the text $B$1:$B$1000 for example. Then you could use INDIRECT("dAc"&A1) where A1 contains 5000.
    Brilliant! Your suggestion works if I do the following:

    Workable solution:

    Use INDIRECT("dAc"&$C:$C)
    where $C:$C is a column of account numbers (eg 5000)
    Define dAc5000 as =accts!$N$627
    where $N$627 contains =$V$111:$V$142 (a range in an external data file corresponding to account 5000)

    Although I must admit I'm still unclear as to why the following solutions don't work, when the name dAc5000, as defined, returns the same range (eg $V111:$V$142) in each case:

    Non-Working Solution 1:

    Use INDIRECT("dAc"&$C:$C) (as above)
    Define dAc5000 as ="$V$111:$V$142"

    Non-Working Solution 2:

    Use INDIRECT("dAc"&$C:$C) (as above)
    Define dAc5000 as =INDEX(dAcctRange,MATCH(5000,dAcctId))
    where
    dAcctRange is =accts!$N:$N, column containing data file ranges (eg $V$111:$V$142)
    dAcctId is =accts!$A:$A, column containing account numbers (eg 5000)

    In each case, the name dAc5000 returns the same range, $V$111:$V$142, but only the first solution works. Even a seemingly more direct method (Non-Working Solution 1 - name defined as range) does not work while the less direct method does (Workable Solution - name defined as cell that contains range).

    Is this just a quirk of the INDIRECT function or is there something a little more obvious that I'm just not understanding about the this function?

  10. #10
    Forum Guru romperstomper's Avatar
    Join Date
    11-04-2008
    Location
    A1
    MS-Off Ver
    Most
    Posts
    12,302

    Re: Return range name from concatenated strings

    As I mentioned, INDIRECT will only work with a named range (your example 1 is a named constant - a string - not an actual range). It will also only work with static named ranges, not dynamic, which is why example 2 fails. If you need to work with a dynamic range, you can use EVALUATE but that can only be used in a defined name, not in a worksheet cell directly.
    Last edited by romperstomper; 03-30-2010 at 03:32 AM. Reason: spelling

  11. #11
    Registered User
    Join Date
    03-29-2010
    Location
    Earth
    MS-Off Ver
    Excel 2007
    Posts
    37

    Re: Return range name from concatenated strings

    Thanks for the explanation and all your help with this issue. I've learned a lot. It's much appreciated.

+ 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