Sub topic 12
Shaping Arrays with TAKE, DROP, VSTACK and HSTACK
This chapter assumes you can enter formulas, understand a spill range and its anchor cell, and have used FILTER, SORT and UNIQUE. You need Excel for Microsoft 365 or a version that includes the newer array shaping functions. TAKE, DROP, VSTACK, HSTACK and CHOOSECOLS are not in Excel 2021, so check before relying on them.
Why shaping functions exist
The core array functions decide which values you get. Shaping functions decide the size and arrangement of the result. They let you cut an array down, remove parts of it, or join several arrays into one. Their output spills like any other array result, so the spill rules from earlier chapters still apply, including #SPILL! when the target cells are blocked.
| Function | What it does | Typical use |
|---|---|---|
| TAKE | Keeps rows or columns from the start or end | Top N, last N |
| DROP | Removes rows or columns from the start or end | Skip headers or totals |
| VSTACK | Joins arrays top to bottom | Combine lists or tables |
| HSTACK | Joins arrays side by side | Add a computed column |
| CHOOSECOLS | Picks columns by position | Reorder or select columns |
TAKE: keep part of an array
The syntax is TAKE(array, rows, [columns]). A positive number counts from the start. A negative number counts from the end.
The formula below sorts a two-column range by its second column in descending order. It keeps the first three rows, which gives you a top 3 list.
=TAKE(SORT(A2:B20,2,-1),3)Some variations:
=TAKE(A2:B20,-3)
=TAKE(A2:B20,,1)
=TAKE(A2:B20,2,1)- The first formula returns the last three rows.
- The second returns only the first column. The empty second argument means "all rows".
- The third returns the top-left 2 by 1 block.
If you ask for more rows than the array has, TAKE returns all of them and does not raise an error. This makes TAKE(x,10) safe when the data may have fewer than 10 rows.
DROP: remove part of an array
The syntax is DROP(array, rows, [columns]). It uses the same sign convention as TAKE, but it removes cells instead of keeping them.
=DROP(A1:C20,1)This removes the header row and returns the 19 data rows. A negative number removes from the end, which is useful for a totals row:
=DROP(A1:C20,-1)You can drop rows and columns in one call. =DROP(A1:C20,1,1) removes the first row and the first column.
If you drop every row or column, the result is empty and Excel returns #CALC!. This is the same empty-array error you met with FILTER.
VSTACK: join arrays vertically
VSTACK(array1, [array2], ...) places each array below the previous one. This lets you combine lists from several sheets without copying.
=VSTACK(A2:B10, D2:E15)The result has 9 + 14 = 23 rows. You can also stack a header on top of a computed result:
=VSTACK({"Name","Amount"}, FILTER(A2:B20, B2:B20>100, "No matches"))The braces create an array constant. It holds one row with two text values, and it becomes the header of the spill.
Mismatched widths
If the arrays have different numbers of columns, VSTACK pads the narrower ones with #N/A. For example, stacking a 2-column array on a 3-column array puts #N/A in the third column of the first rows. You can hide the padding by wrapping the call:
=IFNA(VSTACK(A2:B10, D2:F15), "")Note that IFNA also hides real #N/A values in your data, so use it only when you know the padding is the only source.
HSTACK: join arrays horizontally
HSTACK(array1, [array2], ...) places arrays side by side. Rows that are missing in a shorter array are padded with #N/A.
=HSTACK(A2:A6, B2:B6*C2:C6)This returns the original names in the first column and a computed product beside them. The result is a single spill range with two columns. You do not need a helper column.
Combine both stacking functions to build a table with a header and a computed column:
=VSTACK(
HSTACK("Item","Total"),
HSTACK(A2:A6, B2:B6*C2:C6)
)Because HSTACK and VSTACK take arrays, you can pass in the output of any function that returns one, including FILTER and SORT.
CHOOSECOLS: pick columns by position
CHOOSECOLS(array, col1, [col2], ...) returns the columns you name, in the order you name them. A negative number counts from the right.
=CHOOSECOLS(A2:D20, 4, 1)This returns column 4 first and column 1 second. Use -1 for the last column when the width of the data may change:
=CHOOSECOLS(A2:D20, 1, -1)A column number of 0 or larger than the array width returns #VALUE!.
How the pieces fit together
Shaping functions are most useful at the outer edge of a formula. You compute, filter and sort first. You then trim and attach headers at the end.
A complete example, a top 5 report with a header:
=VSTACK(
{"Product","Sales"},
TAKE(SORT(FILTER(A2:B100, B2:B100>0, "No matches"), 2, -1), 5)
)If FILTER finds no rows, it returns the text No matches as a single value. TAKE and SORT pass that text through, so the report shows the header and one padded row. In that case the second column contains #N/A. When this matters, test the count first with IF(COUNTIF(B2:B100,">0")=0, "No matches", ...).
Common errors
| Symptom | Cause | Fix |
|---|---|---|
| `#SPILL!` | Cells below or beside the anchor cell are not empty | Clear the blocking cells |
| `#CALC!` from DROP or TAKE | The result would have zero rows or columns | Check the size, or wrap with `IFERROR` |
| `#N/A` in stacked output | Arrays have different widths or heights | Match sizes, or use `IFNA` |
| `#VALUE!` from CHOOSECOLS | A column index is 0 or too large | Use an index from 1 to the array width |
| `#NAME?` | Your Excel version lacks the function | Use an older approach such as INDEX |
Referring to a shaped result
A shaped formula is still a spill range. Reference it from another cell with the # operator:
=SUM(E2#)If E2 holds a TAKE formula and you change N from 3 to 5, the sum updates with no edit.
Choosing between the functions
- Use TAKE when you know how many rows you want to keep, such as a top 10.
- Use DROP when you know what to remove, such as a header, a totals row or a notes column.
- Use VSTACK to combine sources with the same columns.
- Use HSTACK to attach computed columns without helper cells.
- Use CHOOSECOLS to select or reorder columns when the table has many.
For long formulas, name the intermediate array with LET so the logic reads in order. Naming also avoids calculating the same sort twice.
=LET(
sorted, SORT(A2:B100, 2, -1),
VSTACK({"Product","Sales"}, TAKE(sorted, 5))
)Exercise
A table in A1:C50 has a header row in row 1 and a totals row in row 50. Write one formula that returns only the data rows, with the first two columns only, and no header or totals.
Show answer
=TAKE(DROP(A1:C50,1,0),-48...
Exercise
A table in A1:C50 has a header row in row 1 and a totals row in row 50. Write one formula that returns only the data rows (rows 2 to 49) and only the first two columns.
Show answer
=TAKE(DROP(A1:C50,1,0),48,2) works, but a shorter form is =TAKE(DROP(A1:C50,1,-1),-48). The simplest is =CHOOSECOLS(DROP(A1:C50,1,0),1,2) combined with DROP(...,-1). For example: =CHOOSECOLS(DROP(DROP(A1:C50,1),-1),1,2). First DROP removes the header, the second removes the totals row, and CHOOSECOLS keeps columns 1 and 2.
Exercise
What does =VSTACK(A2:B4, D2:F4) return in the third column for the first three rows, and why?
Show answer
It returns #N/A. The first array has two columns and the second has three, so VSTACK pads the narrower array to the width of the widest one with #N/A.