A key to many Excel formulas is the ability to return an array of integers {1;2;...;n}. One of my hopes with each new release has been that MS would include a function that did this for you like =INTARRAY(n). It's easy to write such a function in VBA but then you may have to worry about distributing a workbook containing macros.
One basic method that you often see for a given Range is to use:
=ROW(Range)-MIN(ROW(Range))+1
While not totally ideal, this works fine for most purposes.
Q. But how about if you want to construct an array that is not based on a range?
AFAIK there are three basic options available:
=ROW(INDIRECT("1:"&intCount))
=ROW(OFFSET(A1,,,intCount))
=ROW(INDEX(A:A,1):INDEX(A:A,intCount))
These all do the job but there are unwanted side effects in that they add unnecessary references and/or volatility. This means that the formula:
a) may be sensitive to worksheet operations such as row deletion or insertion and recalculate
b) recalculates either every time the range that is referred to changes (eg: A:A in the last case) or every time any cell changes in the case of the first two. All three of these recalculate at start up causing the save changes prompt to appear by default even if no changes have been made to the workbook.
Q. So what other alternatives are there?
Until now i didn't think there were any but I just stumbled upon a way to return an array of numbers given only an array of text items or even just a number without needing to refer to a range. This method allows:
In Excel 2010 and prior, to return an array of numbers based on an array.
eg: using {"apple,"pear","banana","orange"} return {1;2;3;4}
In Excel 2013 just given a number, to return an array of numbers of that size.
eg: using 4 to return {1;2;3;4}
I had thought of entering this as a challenge but decided that it was more of theoretical interest. I would be interested to know any other ideas people have for this.
One basic method that you often see for a given Range is to use:
=ROW(Range)-MIN(ROW(Range))+1
While not totally ideal, this works fine for most purposes.
Q. But how about if you want to construct an array that is not based on a range?
AFAIK there are three basic options available:
=ROW(INDIRECT("1:"&intCount))
=ROW(OFFSET(A1,,,intCount))
=ROW(INDEX(A:A,1):INDEX(A:A,intCount))
These all do the job but there are unwanted side effects in that they add unnecessary references and/or volatility. This means that the formula:
a) may be sensitive to worksheet operations such as row deletion or insertion and recalculate
b) recalculates either every time the range that is referred to changes (eg: A:A in the last case) or every time any cell changes in the case of the first two. All three of these recalculate at start up causing the save changes prompt to appear by default even if no changes have been made to the workbook.
Q. So what other alternatives are there?
Until now i didn't think there were any but I just stumbled upon a way to return an array of numbers given only an array of text items or even just a number without needing to refer to a range. This method allows:
In Excel 2010 and prior, to return an array of numbers based on an array.
eg: using {"apple,"pear","banana","orange"} return {1;2;3;4}
In Excel 2013 just given a number, to return an array of numbers of that size.
eg: using 4 to return {1;2;3;4}
I had thought of entering this as a challenge but decided that it was more of theoretical interest. I would be interested to know any other ideas people have for this.