Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use INDEX() inside ARRAYFORMULA()?

I am trying to use the INDEX() formula inside an ARRAYFORMULA(). As a simple (non-sense) example, with 4 elements in column A, I expected that the following array formula entered in B1 would display all four elements from A in column B:

=ARRAYFORMULA(INDEX($A$1:$A$4,ROW($A$1:$A$4)))

However, this only fills field B1 with a the value found in A1.

When I enter

=ARRAYFORMULA(ROW($A$1:$A$4))

in B1, then I do see all numbers 1 to 4 appear in column B. Why does my first array formula not expand similar like the second one does?

like image 625
Reinier Torenbeek Avatar asked Apr 22 '13 06:04

Reinier Torenbeek


People also ask

Can INDEX return an array Google Sheets?

The INDEX function will return the cell or array of cells at the intersection of the specified row and column of the specified range.

Can you use concatenate in Arrayformula?

Unfortunately, CONCATENATE nested with ARRAYFORMULA will return all values from columns merged in a row. But you can use an ampersand ( & ) instead of CONCATENATE and nest it within ARRAYFORMULA.

How do you use Arrayformula?

To use it in Google Sheets, you can either directly type “ARRAYFORMULA” or hit a Ctrl+Shift+Enter shortcut (Cmd + Shift + Enter on a Mac), while your cursor is in the formula bar to make a formula an array formula (Google Sheets will automatically add ARRAYFORMULA to the start of the formula).


1 Answers

The INDEX function is one that does not support "iteration" over an array if an array is used as one of its arguments. There is no documentation of this that I know of; it simply is what it is. So the second argument will always default to the first element of the array, which is ROW(A1).

One clumsy workaround to achieve what you require relies on a second adjacent column existing next to the source data* (although it is unimportant what values are actually in that second column):

=ArrayFormula(HLOOKUP(IF(ROW($A$1:$A$4);$A$1);$A$1:$B$4;ROW($A$1:$A$4);0))

or indeed something like:

=ArrayFormula(HLOOKUP(IF({3;2;4;1};$A$1);$A$1:$B$4;{3;2;4;1};0))


edit 2015-06-09

* This is no longer a requirement in the newest version of Sheets; the second argument in the HLOOKUP can just be $A$1:$A$4.

like image 97
AdamL Avatar answered Oct 14 '22 00:10

AdamL