Summary

The Excel BYCOL function applies a function to each column in a given array and returns one result per column. BYCOL can apply stock functions like SUM, COUNT, and AVERAGE or a custom LAMBDA function. All results are returned at the same time in a single array.

Purpose 

Apply function to column

Return value 

One result per column

Syntax

=BYCOL(array,lambda)
  • array - The array or array to process.
  • lambda - The lambda function to apply to each column.

How to use 

The Excel BYCOL function applies a function to each column in array and returns one result per column in a single array. The purpose of BYCOL is to process data in an array or range in a "by column" fashion. For example, if BYCOL is given an array with 10 columns, BYCOL will return an array with 10 results. The calculation performed on each column can be a stock function like SUM, COUNT, etc., or a custom LAMBDA function designed to operate on column values. BYCOL can accept an abbreviated "eta lamba" syntax for simple operations like SUM, as explained below.

The concept and syntax

Essentially, BYCOL allows you to loop over all columns in a range or array and perform a custom calculation on each column in a single formula. The second argument, function, determines the calculation performed:

=BYCOL(array,function)

The function can be provided in two ways: a short-form "eta lambda" syntax or a long-form custom LAMBDA syntax. For example, to sum each column in an array, the short-form syntax looks like this:

=BYCOL(array,SUM)

The long-form custom syntax looks like this:

=BYCOL(array,LAMBDA(column,SUM(column))

Why two syntax options? The short form is for convenience. It is concise and easy to read. However, the behavior cannot be customized. The long-form syntax uses the LAMBDA function and can be customized as desired. See below for an example of BYCOL formulas that use both options.

Note: in the examples above, we use the variable name "column" in the LAMBDA function for clarity. However, you are free to use whatever name you like.

Example - sum all columns

In the worksheet below, the BYCOL function is used to sum the values in each column of the range C5:H14. The formula in cell C16 looks like this:

=BYCOL(C5:H14,LAMBDA(column,SUM(column)))

BYCOL function example - sum all columns

The BYCOL function returns all sums at once in a single array. Because there are 6 columns in the range, the result is an array that contains 6 sums like this:

{8859,8690,9020,9112,8876,9153}

The values in this array spill into the range C16:H16. The formulas below are other examples of how BYCOL can be used on the same data with formulas that follow the same pattern:

=BYCOL(C5:H14,LAMBDA(column,MAX(column))) // max
=BYCOL(C5:H14,LAMBDA(column,MIN(column))) // min
=BYCOL(C5:H14,LAMBDA(column,AVERAGE(column))) // average

Example - Short form "eta lambda" syntax

While BYCOL is designed to accept a function defined by LAMBDA, it will also accept a short-form "eta lambda" syntax for simple operations. Using the eta lambda syntax, you can pass a function by name only into BYCOL like this:

=BYCOL(array,SUM)

This formula is equivalent to the long-form version:

=BYCOL(array,LAMBDA(x,SUM(x)))

For example, in the worksheet below, we have adapted the formula in J5 to use the short-form syntax. The formula now looks like this:

=BYCOL(C5:H14,SUM)

BYCOL function example - sum columns with short-form syntax

The Excel formula engine knows how to pass each column into the SUM function, and the results are exactly the same as the long-form version. You can use the same syntax to call other functions like this:

=BYCOL(array,SUM) // sum each column
=BYCOL(array,MAX) // max of each column
=BYCOL(array,MIN) // min of each column
=BYCOL(array,COUNT) // count of each column

The short-form eta syntax works for functions that accept a single argument, such as SUM, AVERAGE, MIN, MAX, COUNT, COUNTA, etc. It will not work in all scenarios because the logic can't be modified. For example, the formula in the next section below can't use the eta lambda syntax.

Example - Count values over 900

In this example, the goal is to count the values in each column greater than 900. The formula is a bit more complex because we need more logic. This is a situation where we can't use the short-form syntax above. The formula in cell C16 looks like this:

=BYCOL(C5:H14,LAMBDA(column,SUM(--(column>900))))

BYCOL function example - count values over 900

Working from the inside out, a custom lambda counts all values in a column greater than 900:

LAMBDA(column,SUM(--(column>900))) // custom lambda

The logical expression column>900 returns an array of TRUE and FALSE values, and we use a double negative (--) to coerce these values to 1s and 0s. The SUM function then sums the 1s and 0s, and the result is the count of values greater than 900:

SUM(--(column>900))

The LAMBDA runs on each column in the data. Since there are 6 columns in the range C5:H14, BYCOL returns 6 results that spill into the range C16:H16. See Boolean operations in array formulas for more information about the logic inside of SUM. 

You could use COUNTIFS instead of SUM to solve this problem. However, COUNTIFS requires a range, which means you can't give BYCOL an array if you use COUNTIFS in the calculation. For this reason, I generally avoid the *IFS functions when working with the new dynamic array functions where arrays are more common.

Example - BYCOL with multiple functions

It is also possible to configure BYCOL to run more than one function at the same time, although it is not obvious how. The trick is to wrap the functions in the HSTACK or VSTACK functions (depending on the layout needed). For example, in the worksheet below, BYCOL is configured to return the sum, the maximum value,  the minimum value, and the average value for each column at the same time. The formula in C12, copied across, is:

=BYCOL(C5:H10,VSTACK(SUM,MAX,MIN,AVERAGE))

BYCOL function example - multiple calculations at the same time

Although this formula uses the short-form syntax, you can take the same approach with custom LAMBDA calculations. 

Because of the current array of arrays limitation in Excel, this formula will not spill down across all columns. It will initially only calculate results for the first column. You will then need to copy the formula manually to all columns in a second step.

Dave Bruns Profile Picture

AuthorMicrosoft Most Valuable Professional Award

Dave Bruns

Hi - I'm Dave Bruns, and I run Exceljet with my wife, Lisa. Our goal is to help you work faster in Excel. We create short videos, and clear examples of formulas, functions, pivot tables, conditional formatting, and charts.