Sub topic 12
Shaping arrays with TAKE, DROP, VSTACK and HSTACK
This chapter assumes you can enter formulas, use cell and range references, and already know how SORT, FILTER and the spill range operator (#) work. You need Excel for Microsoft 365 or a version that includes TAKE, DROP, VSTACK and HSTACK. Excel 2021 does not include them.
Why shape arrays
Array functions return blocks of values. Often you want only part of a block, or you want to join two blocks into one. The shaping functions do this without helper columns or copied ranges. Each one returns an array, so the result spills like any other.
TAKE and DROP
TAKE keeps rows or columns from the start or end of an array. DROP removes them and keeps the rest.
The syntax is the same for both:
TAKE(array, rows, [columns])DROP(array, rows, [columns])
A positive number counts from the top or left. A negative number counts from the bottom or right.
The following formula sorts a two-column range by its second column in descending order and keeps only the first three rows. It is a top-3 list:
=TAKE(SORT(A2:B20,2,-1),3)SORT runs first and produces the ordered rows. TAKE then cuts that result to three rows. Changing the 3 to -3 returns the three lowest rows instead, because the sort puts the smallest values last.
DROP is useful for removing a header row from a range before you process the data:
=DROP(A1:C20,1)This returns rows 2 to 20 and spills 19 rows by 3 columns. To skip the first row and also remove the last column, pass a column count too: =DROP(A1:C20,1,-1).
VSTACK and HSTACK
VSTACK appends arrays vertically, one below the other. HSTACK appends them horizontally, side by side.
=VSTACK(A2:B10,D2:E6)This returns the nine rows of A2:B10 followed by the five rows of D2:E6, giving 14 rows and two columns. Each source can be a range or the result of another array function.
=HSTACK(A2:A10,B2:B10*C2:C10)This places the original column A next to a calculated column. The calculated column is the product of B and C on each row. You can build a table with a computed column and no extra formulas.
If the arrays differ in size, the shorter one is padded with #N/A in the empty positions. VSTACK pads missing columns, and HSTACK pads missing rows. Wrap the result in IFERROR or IFNA to replace the padding:
=IFNA(VSTACK(A2:C10,D2:E6),"")How the pieces fit
The functions chain from the inside out. The diagram shows one common pipeline.
The matching formula is:
=TAKE(SORT(VSTACK(A2:B10,D2:E6),2,-1),3)Common errors
- #VALUE! from TAKE or DROP: the row or column count is larger than the array in a way the function cannot handle, or the argument is not a number. TAKE accepts a count larger than the array and returns everything, so check for text in the argument.
- #CALC! from TAKE or DROP: the result would be empty. For example, dropping all rows of an array leaves nothing to return. Use a smaller count.
- #SPILL!: the output needs more space than is free. Clear the cells in the way, as covered in the chapter on #SPILL!.
- Unwanted #N/A cells: these come from stacking arrays of different widths or heights. Match the sizes or use IFNA.
Exercise
Exercise
Range A2:B30 holds product names in column A and sales in column B. Write one formula that returns the five products with the lowest sales, smallest first.
Show answer
=TAKE(SORT(A2:B30,2,1),5). SORT with sort index 2 and order 1 sorts by sales in ascending order, so the lowest values come first. TAKE then keeps the first five rows. The result spills as 5 rows by 2 columns.
Exercise
A2:B10 and D2:E6 are two lists with the same two columns. Write a formula that combines them into one list, with no #N/A values.
Show answer
=VSTACK(A2:B10,D2:E6). Both ranges have two columns, so no padding is needed and the result is 14 rows by 2 columns. If the widths differed, wrap the formula in IFNA(...,"").