How to use the CHOOSECOLS and CHOOSEROWS Functions in Excel to Extract Data
Excel’s CHOOSECOLS and CHOOSEROWS functions are like twins: their DNAs are very similar, but they’re separated by subtle differences. The same can be said for their syntaxes.
Here’s the syntax for CHOOSECOLS:
=CHOOSECOLS(a,b,c,…)
And here’s the syntax for CHOOSEROWS:
=CHOOSEROWS(a,b,c,…)
In both cases,
- a (required) is the original array that contains the columns (CHOOSECOLS) or rows (CHOOSEROWS) you want to extract,
- b (required) is the index number of the first column (CHOOSECOLS) or row (CHOOSEROWS) to be extracted,
- c (optional) represents the index numbers of any additional columns (CHOOSECOLS) or rows (CHOOSEROWS) to be extracted, each of which must be separated by commas.
If an index number represents a column or row in the center of the array (in other words, not the first or last column or row), adding extra columns or rows to the center of the array will change the indexed column or row.
You might be thinking that these functions sound very similar to the TAKE function. However, where TAKE lets you extract the first or last x columns or rows, or a single column or row from elsewhere in a named table, CHOOSECOLS and CHOOSEROWS let you extract any number of columns in any order from anywhere in your data.
Example 1: Extracting the First and Last Column or Row From a Table
I’ve already lost count of the number of times I’ve used CHOOSECOLS and CHOOSEROWS to extract the first and last column or row from a table. This is particularly handy if the first column or row is a header, and the last column or row contains totals.
To follow along as you read this guide, download a free copy of the Excel workbook used in the examples. After you click the link, you’ll find the download button in the top-right corner of your screen.
Imagine you’re an administrator for a local sports league, and you’re producing a report that summarizes the points scored by five teams across five games.
The first dataset you want to generate will tell you the total number of points scored overall by each team. To do this, in a blank cell, type:
=CHOOSECOLS(T_Games[#All],1,-1)
where
and press Enter.
By default, CHOOSECOLS counts from left to right, and CHOOSEROWS counts from top to bottom. To reverse this, place the minus symbol (-) in front of the relevant index numbers. Here’s the result you get when you press Enter, and this data can be duplicated on another sheet in the same workbook, such as a dashboard tab, or copied and pasted as text in an email or Word document.
Now, even if more data is added, the result—including the Total row—updates accordingly.
You could also use the same method with unformatted tables by using direct cell references rather than structured references. However, if you add more rows to the right or columns to the bottom of your array, the formula won’t pick these up unless you change the cell references manually. Also, generally speaking, tables offer better tools and adaptability than unstructured cells.
The next report you want to generate will show the number of points scored in each game (rows 1 and 8).
So, in a blank cell, type:
=CHOOSEROWS(T_Games[#All],1,-1)
Example 2: Extracting Columns from more than one Range
Now, let’s say you have this spreadsheet, and your aim is to produce a list containing the total scores of each team across Leagues 1 (green), 2 (blue), and 3 (gray).
This time, you don’t want Excel to extract the column headers and row totals, since you’re going to nest the VSTACK function within the CHOOSECOLS formula to stack the three tables directly on top of each other. So, in cells I1 and J1, create the column headers manually.
How to Combine, Reshape, and Resize Arrays in Excel
Take control of the arrays in your spreadsheet and arrange them as you please.
=CHOOSECOLS(VSTACK(League_1,League_2,League_3),1,-1)
where
and press Enter.
At this point, you could go one step further and sort the result in descending order by typing:
=SORTBY(I2#,J2:J16,-1)
into cell L2, and pressing Enter.
So far, I’ve shown you the benefits of using CHOOSECOLS and CHOOSEROWS to extract the first and last columns and rows from an array. However, this time, I’ll show you how you can use CHOOSECOLS to extract other columns, and combine this with additional Excel tools to make your spreadsheet stand out.
First, type a game number into cell B9, so that you have something to work with when you generate your CHOOSECOLS formula. Then, in cell A11, type:
=CHOOSECOLS(T_Scores[[#Data],[#Totals]],1,B9+1)
where
- CHOOSECOLS is the formula used to extract columns,
- T_Scores is the name of the table, and [[#Data],[#Totals]] tells Excel to include the data and the totals in the result, but not the header row,
- 1 represents the first column (“Totals”), and
- B9+1 tells Excel that the second index argument is represented by the value in cell B9, plus one. The reason you need to include +1 here is because the game numbers start in the second column of the table. As a result, typing 3 into cell B9 extracts the data from the fourth column of the table, which is game 3.
Excel returns a #VALUE error if any of the index numbers are zero or exceed the number of columns or rows in the array.
This is a good case for using data validation in cell B9 to create a drop-down list of the available game numbers. It beats typing in the same options 200 different times manually.
My preferred way to do this for a header row in an Excel table is to first select all the relevant cells, plus a few extra to allow for growth, and name the range in the name box in the top-left corner of the Excel window. Notice how I’ve not selected the column header “Team,” because I don’t want this to appear in the game drop-down list in cell B9.
- Now, in the Data Validation dialog box, select “List” in the Allow field, make sure “Ignore Blank” is checked, type an equal symbol (=) followed by the name you just gave to the column header range, and click “OK.”
You can now click the drop-down arrow in cell B9 to select a valid game number, safe in the knowledge that even if you add extra columns to your table, CHOOSECOLS and the named range in data validation will work together to update the available options accordingly.
Finally, to visualize your data even further, select cells B11 to B15, and in the Home tab on the ribbon, click “Conditional Formatting,” hover over “Data Bars,” and choose a solid fill color.
As a result, not only have you extracted specific columns from your data using CHOOSECOLS, but you’ve also made the result dynamic by adding a drop-down list, and you’ve formatted the data to add visual clarity.
[mai mult...]