Explanation

In this example, the goal is to join two one-dimensional ranges together horizontally. This can be done with the CHOOSE function and array constant .

The CHOOSE function

The CHOOSE function is used to select arbitrary values by numeric position. CHOOSE is a flexible function and accepts a list of text values, numbers, cell references, in any combination. For example, if we have the colors “red”, “blue”, and “green”, we can use CHOOSE like this:

=CHOOSE(1,"red", "blue", "green") // returns "red"
=CHOOSE(2,"red", "blue", "green") // returns "blue"
=CHOOSE(3,"red", "blue", "green") // returns "green"

If we give CHOOSE an array constant like {1,2}, CHOOSE will return the first and second values in an array at the same time:

=CHOOSE({1,2}},"red", "blue", "green") // returns {"red","blue"}

The result is an array that contains two values and, in the dynamic array version of Excel , these values spill onto the worksheet into the range G5:H16.

Applications

Traditionally, the use of CHOOSE function to combine ranges is used up in tricky array formulas . The formulas below are good examples:

  • VLOOKUP case-sensitive
  • VLOOKUP multiple criteria

In these formulas, the CHOOSE function is used to create a new table (in memory) that can be used by the VLOOKUP function to workaround a difficult problem.

Note: the forthcoming HSTACK function will make this use of CHOOSE unnecessary.

Explanation

The goal is to count the number of cells in a given range, regardless of whether the cells are empty or not. Although Excel has several functions designed to count cells based on their contents, there is no built-in function for counting the total number of cells in a range. The classic solution is to use a formula based on the ROWS function and the COLUMNS function. It is also possible to force the COUNTA function to count all cells, empty or not. Both approaches are explained below.

ROWS + COLUMNS

One way to count all cells in a range is to use the ROWS function with the COLUMNS function in a formula like this:

=ROWS(range)*COLUMNS(range)

This is the approach used in the worksheet shown, where the formula in cell J5 is:

=ROWS(B4:H16)*COLUMNS(B4:H16)

The ROWS function returns the count of rows in a range. Since there are 12 rows in the range B4:H16, ROWS returns 12. In the same way, the COLUMNS function returns the count of columns in a range, which is 7 in this case. The formula is evaluated by Excel like this:

=ROWS(B4:H16)*COLUMNS(B4:H16)
=12*7
=91

Count all cells in a worksheet

The ROWS and COLUMNS functions can work with a rectangular range of any size. For example, the formula below uses the ROWS function to count all rows in column A, and the COLUMNS function to count all columns in row 1:

=ROWS(A:A)*COLUMNS(1:1) // count all cells in worksheet

ROWS returns a count of 1,048,576, and COLUMNS returns a count of 16,384, so the final result is 17,179,869,184. This is the total number of cells in an Excel worksheet. The formula below uses the range 1:1048576 to achieve the same result:

=ROWS(1:1048576)*COLUMNS(1:1048576) // returns 17179869184
 

Tip: Excel will enter the range 1:1048576 for you if you click the upper-left corner of the spreadsheet while entering a formula.

COUNTA alternative

If you find the ROWS + COLUMNS formula cumbersome, you can try the formula below, which is based on the COUNTA function:

=COUNTA(range+0)

This formula is a bit of a hack. The COUNTA function will count cells that contain numbers, text, or errors. It will not count empty cells. If we use COUNTA with the range itself, we get a result of 82, since the range contains 9 empty cells:

=COUNTA(B4:H16) // returns 82

However, when we add zero:

=COUNTA(B4:H16+0)

The match operation will cause Excel to evaluate the empty cells as zeros, and it will return a combination of numbers and errors. In this example, the array created inside COUNTA looks like this:

=COUNTA({0,18,46,54,#VALUE!,23,13;10,#VALUE!,76,#VALUE!,64,14,64;44,34,0,39,30,0,#VALUE!;81,90,#VALUE!,58,#VALUE!,#VALUE!,75;32,79,56,14,1,9,70;79,#VALUE!,8,0,92,64,#VALUE!;#VALUE!,56,89,18,42,43,44;48,38,#VALUE!,68,#VALUE!,0,91;32,0,59,#VALUE!,1,#VALUE!,#VALUE!;41,#VALUE!,39,55,72,0,57;61,68,#VALUE!,#VALUE!,18,87,#VALUE!;80,65,48,96,5,36,0;11,92,#VALUE!,36,#VALUE!,0,65})

Notice that empty cells are now zero (0), and errors represent cells that contain text. Because all cells represented by the array have a value, COUNTA will now return the same result as the original formula above, 91.

Caution: this formula will work fine on fine on typical size ranges. However, the performance will suffer on very large ranges, and you might notice a delay when the formula is entered or recalculated.