Home > Net >  How to use named range in an Excel array?
How to use named range in an Excel array?

Time:10-16

Basically the problem boils down to - how do I use a named reference/range within array in an Excel spreadsheet formula?

Example:

={"this","is","my","house"}

Produces 4 cells in one row with correct text

But this

={"this","is","my", House}

where House is a named range of a cell containing some text fails.

CodePudding user response:

Your attempt failed, because of the array notation {}. An array, typed like that is limited to number values and/or text strings, for instance {"a",1,"b"}. A range can not be used inside an array notation neither can a named range.

To avoid the array notation and still get the array to include the named range you can make use of VSTACK or HSTACK who both create arrays or append arrays even.

In this case your array {"this","is","my"} can be used inside HSTACK and the named range House can be appended: =HSTACK({"this","is","my"},House)

This will give the desired result, but as HSTACK creates arrays by appending numerous values/ranges/arrays, we no longer need the {}:

Proper notation: =HSTACK("this","is","my", House)

Would be the proper notation.

If you would not have access to HSTACK, but have access to LET, you could use this little more complex solution:

=LET(a,{"this","is","my"},
     b,House,
     count_a,COUNTA(a),
     seq,SEQUENCE(1,count_a 1),
CHOOSE(IF(seq<=count_a,1,2),a,b))

First a (the text array) and b (the named range) are declared. Then count_a is declared, which counts the number of strings in array a (3). Then seq is declared to create a (horizontal) sequence from 1 to the count of strings in a (count_a) and adding 1 (resulting in {1,2,3,4}.

Then calculating if the sequence seq is smaller than or equal to the count of strings in a results in TRUE for the first 3 values of the sequence and false for the fourth: {TRUE,TRUE,TRUE,FALSE}. Using that in combination with IF (if TRUE 1, else 2) results in an array of {1,1,1,2}. Using that as the CHOOSE argument results in the 1st 3 times choosing values from a and the 4th time (the first) value of named range b.

Not using LET and SEQUENCE will result in a very unmanageable formula, which would require more work fixing the values within the formula then just typing them out, probably, but this would create the array in older Excel versions:

=CHOOSE( 
        IF(
           COLUMN($A$1: 
                  INDEX($1:$1048576,,COUNTA({"this","is","my"}) 1))
 
           <=COUNTA({"this","is","my"}),
           1,
           2),
        {"this","is","my"},
        House)

Requires entered with ctrl shift enter and would appear as being one value only, because older Excel doesn't spill arrays into a range, but the array could be referenced inside a formula or as a named range.

Here COLUMN($A$1:INDEX($1:$1048576,,COUNTA({1,2,3}))) simulates the sequence function.

CodePudding user response:

If you have access to Excel O365:

With HSTACK, it would simply be =HSTACK("this","is","my",house).

House could be a single value or an array. If "House" is a named range {"A","B,"C"} then the HSTACK function above returns a 6 element array {"this","is","my","A","B,"C"}

  • Related