How to use the BYCOL function
What is the BYCOL function?
The BYCOL function passes all values in a column based on an array to a LAMBDA function, the LAMBDA function calculates new values based on a formula you specify. It then continues with the next column until all columns in the array have been processed.
Table of Contents
1. Introduction
What is an array in Excel?
An array in Excel is a collection of values arranged in rows and columns. It can be thought of as a table or a grid of data.
There are two types of arrays. One-dimensional arrays: A single row or column of data. Two-dimensional arrays: Data organized in both rows and columns.
There are two types of array formulas: The first type returns a single value and the second type returns multiple values. An array formula is a formula that can perform multiple calculations on one or more sets of values.
Excel 365 subscribers have access to dynamic array formulas, a powerful feature that automatically adjusts its output range. These formulas populate the initial target cell and expand into neighboring cells as needed, adapting their size based on the formula's result. This automatic expansion and contraction of the output range is the key characteristic that gives them the name "dynamic" array formulas. The process of extending results into adjacent cells is known as "spilling.
Excel processes arrays in RAM allowing for rapid computations. However, when array sizes exceed available memory, Windows may resort to using virtual memory on the hard drive or SSD. This fallback to disk storage significantly slows down calculations, as accessing data from these devices is much slower than from RAM.
What is the LAMBDA function?
The BYCOL function uses the LAMBDA function as its second argument to define how each cell in the array should be calculated. This combination allows you to perform cumulative calculations across a range of values.
The LAMBDA function is required in the BYCOL function, you can't leave it out. Read more: LAMBDA function
2. Syntax
BYCOL(array, lambda(array, calculation))
Argument | Description |
array | Required. A cell reference to a cell range or an array. |
3. How to count the top 10 largest numbers in a cell range per column
Cell range B4:F13 contains some random numbers. The top ten largest numbers out of all numbers in cells B4:F13 are highlighted in blue. Cell H4 contains a formula that counts the top 10 largest numbers per column. In other words, how many top 10 numbers are in each column in B4:F13.
Excel 365 formula in cell B15:
The formula in cell B15 returns a dynamic array that spills values horizontally. It returns the following array: {5, 1, 1, 2, 1} which corresponds to each column in B4:F13.
Here is how the formula works:
- LARGE(B4:F13,10): This function finds the 10th largest value in the range B4:F13.
- B4:F13 >= LARGE(B4:F13,10): This compares each cell in the range B4:F13 to the 10th largest value. It creates a boolean array where each cell is TRUE if it's greater than or equal to the 10th largest value, and FALSE otherwise.
- IF(B4:F13>=LARGE(B4:F13,10),1,0): This part is the condition being checked for each cell in the range B4:F13
IF(condition, value_if_true, value_if_false): This is the IF function, which takes three arguments:a. The condition to check (B4:F13 >= LARGE(B4:F13,10))
b. The value to return if the condition is true (1)
c. The value to return if the condition is false (0)
The IF statement checks if each cell is greater than or equal to this 10th largest value. If true, it returns 1; if false, it returns 0. For each cell in B4:F13, if the value is greater than or equal to the 10th largest value in the range, it returns 1. Otherwise, it returns 0.
This effectively creates a new array of the same size as B4:F13, but filled with only 1s and 0s. - LAMBDA(a,SUM(a)): This is the calculation applied to each column. It sums up the 1s and 0s in each column.
- BYCOL(): This function applies a calculation to each column of a range and returns the results.
Explaining the formula
Step 1 - Calculate the 10th largest number
The LARGE function calculates the k-th largest value from an array of numbers.
Function syntax: LARGE(array, k)
LARGE(B4:F13,10)
becomes
LARGE({527,79,121,76,128;219, ... ,241},10)
and returns 701.
Step 2 - Compare the 10th largest number to all numbers in the cell range
The greater than sign lets you compare values in an Excel formula, this example shows the greater than sign and an equal sign combined.
These characters are logical operators, they return TRUE or FALSE.
B4:F13>=LARGE(B4:F13,10)
becomes
B4:F13>=701
becomes
{527,79,121,76,128;219, ... ,241}>=701
and returns
{FALSE, FALSE, FALSE, FALSE, FALSE; FALSE, ... , FALSE}
Step 3 - Replace boolean values with numerical values
The IF function returns one value if the logical test is TRUE and another value if the logical test is FALSE.
Function syntax: IF(logical_test, [value_if_true], [value_if_false])
IF(B4:F13>=LARGE(B4:F13,10),1,0)
becomes
IF({FALSE, FALSE, FALSE, FALSE, FALSE; FALSE, ... , FALSE},1,0)
and returns
{0, 0, 0, 0, 0; 0, ... , 0}.
Step 4 - Build the LAMBDA calculation
The SUM function allows you to add numerical values, the function returns the sum in the cell it is entered in. The SUM function is cleverly designed to ignore text and boolean values, adding only numbers.
Function syntax: SUM(number1, [number2], ...)
SUM(a)
Step 5 - Build the LAMBDA function
The LAMBDA function build custom functions without VBA, macros or javascript.
Function syntax: LAMBDA([parameter1, parameter2, …,] calculation)
LAMBDA(a,SUM(a))
Step 6 - Pass values to the LAMBDA function
BYCOL(IF(B4:F13>=LARGE(B4:F13,10),1,0),LAMBDA(a,SUM(a)))
becomes
BYCOL({0, 0, 0, 0, 0; 0, ... , 0},LAMBDA(a,SUM(a)))
and returns
{5, 1, 1, 2, 1}.
4. How to count the bottom 10 numbers in a cell range per column
Cell range B4:F13 contains some random numbers. The bottom ten smallest numbers are highlighted yellow.
Cell H4 contains a formula that counts the bottom 10 smallest numbers per column.
Excel 365 formula in cell H4:
Explaining the formula
Step 1 - Calculate the 10th smallest number
The SMALL function returns the k-th smallest value from a group of numbers.
Function syntax: SMALL(array, k)
SMALL(B4:F13,10)
becomes
SMALL({527,79,121,76,128;219, ... ,241},10)
and returns 145.
Step 2 - Compare the 10th smallest number to all numbers in the cell range
The greater than sign lets you compare values in an Excel formula, this example shows the greater than sign and an equal sign combined.
These characters are logical operators, they return TRUE or FALSE.
B4:F13>=SMALL(B4:F13,10)
becomes
B4:F13<=701
becomes
{527,79,121,76,128;219, ... ,241}<=145
and returns
{FALSE, TRUE, TRUE, TRUE, TRUE; FALSE, ... , FALSE}
Step 3 - Replace boolean values with numerical values
The IF function returns one value if the logical test is TRUE and another value if the logical test is FALSE.
Function syntax: IF(logical_test, [value_if_true], [value_if_false])
IF(B4:F13<=SMALL(B4:F13,10),1,0)
becomes
IF({FALSE, TRUE, TRUE, TRUE, TRUE; FALSE, ... , FALSE},1,0)
and returns
{0, 1, 1, 1, 1;0, ... , 0}.
Step 4 - Build the LAMBDA calculation
The SUM function allows you to add numerical values, the function returns the sum in the cell it is entered in. The SUM function is cleverly designed to ignore text and boolean values, adding only numbers.
Function syntax: SUM(number1, [number2], ...)
SUM(a)
Step 5 - Build the LAMBDA function
The LAMBDA function build custom functions without VBA, macros or javascript.
Function syntax: LAMBDA([parameter1, parameter2, …,] calculation)
LAMBDA(a,SUM(a))
Step 6 - Pass values to the LAMBDA function
BYROW(IF(B4:F13<=SMALL(B4:F13,10),1,0),LAMBDA(a,SUM(a)))
becomes
BYROW({0, 1, 1, 1, 1;0, ... , 0},LAMBDA(a,SUM(a)))
and returns
{0, 2, 2, 3, 3}.
5. Function not working
The BYCOL function returns
- #NAME? error if you misspell the function name.
- propagates errors, meaning that if the input contains an error (e.g., #VALUE!, #REF!), the function will return the same error.
5.1 Troubleshooting the error value
When you encounter an error value in a cell a warning symbol appears, displayed in the image above. Press with mouse on it to see a pop-up menu that lets you get more information about the error.
- The first line describes the error if you press with left mouse button on it.
- The second line opens a pane that explains the error in greater detail.
- The third line takes you to the "Evaluate Formula" tool, a dialog box appears allowing you to examine the formula in greater detail.
- This line lets you ignore the error value meaning the warning icon disappears, however, the error is still in the cell.
- The fifth line lets you edit the formula in the Formula bar.
- The sixth line opens the Excel settings so you can adjust the Error Checking Options.
Here are a few of the most common Excel errors you may encounter.
#NULL error - This error occurs most often if you by mistake use a space character in a formula where it shouldn't be. Excel interprets a space character as an intersection operator. If the ranges don't intersect an #NULL error is returned. The #NULL! error occurs when a formula attempts to calculate the intersection of two ranges that do not actually intersect. This can happen when the wrong range operator is used in the formula, or when the intersection operator (represented by a space character) is used between two ranges that do not overlap. To fix this error double check that the ranges referenced in the formula that use the intersection operator actually have cells in common.
#SPILL error - The #SPILL! error occurs only in version Excel 365 and is caused by a dynamic array being to large, meaning there are cells below and/or to the right that are not empty. This prevents the dynamic array formula expanding into new empty cells.
#DIV/0 error - This error happens if you try to divide a number by 0 (zero) or a value that equates to zero which is not possible mathematically.
#VALUE error - The #VALUE error occurs when a formula has a value that is of the wrong data type. Such as text where a number is expected or when dates are evaluated as text.
#REF error - The #REF error happens when a cell reference is invalid. This can happen if a cell is deleted that is referenced by a formula.
#NAME error - The #NAME error happens if you misspelled a function or a named range.
#NUM error - The #NUM error shows up when you try to use invalid numeric values in formulas, like square root of a negative number.
#N/A error - The #N/A error happens when a value is not available for a formula or found in a given cell range, for example in the VLOOKUP or MATCH functions.
#GETTING_DATA error - The #GETTING_DATA error shows while external sources are loading, this can indicate a delay in fetching the data or that the external source is unavailable right now.
5.2 The formula returns an unexpected value
To understand why a formula returns an unexpected value we need to examine the calculations steps in detail. Luckily, Excel has a tool that is really handy in these situations. Here is how to troubleshoot a formula:
- Select the cell containing the formula you want to examine in detail.
- Go to tab “Formulas” on the ribbon.
- Press with left mouse button on "Evaluate Formula" button. A dialog box appears.
The formula appears in a white field inside the dialog box. Underlined expressions are calculations being processed in the next step. The italicized expression is the most recent result. The buttons at the bottom of the dialog box allows you to evaluate the formula in smaller calculations which you control. - Press with left mouse button on the "Evaluate" button located at the bottom of the dialog box to process the underlined expression.
- Repeat pressing the "Evaluate" button until you have seen all calculations step by step. This allows you to examine the formula in greater detail and hopefully find the culprit.
- Press "Close" button to dismiss the dialog box.
There is also another way to debug formulas using the function key F9. F9 is especially useful if you have a feeling that a specific part of the formula is the issue, this makes it faster than the "Evaluate Formula" tool since you don't need to go through all calculations to find the issue..
- Enter Edit mode: Double-press with left mouse button on the cell or press F2 to enter Edit mode for the formula.
- Select part of the formula: Highlight the specific part of the formula you want to evaluate. You can select and evaluate any part of the formula that could work as a standalone formula.
- Press F9: This will calculate and display the result of just that selected portion.
- Evaluate step-by-step: You can select and evaluate different parts of the formula to see intermediate results.
- Check for errors: This allows you to pinpoint which part of a complex formula may be causing an error.
The image above shows cell reference C3:F6 converted to hard-coded value using the F9 key. The BYCOL function requires non-error values which is not the case in this example. We have found what is wrong with the formula.
Tips!
- View actual values: Selecting a cell reference and pressing F9 will show the actual values in those cells.
- Exit safely: Press Esc to exit Edit mode without changing the formula. Don't press Enter, as that would replace the formula part with the calculated value.
- Full recalculation: Pressing F9 outside of Edit mode will recalculate all formulas in the workbook.
Remember to be careful not to accidentally overwrite parts of your formula when using F9. Always exit with Esc rather than Enter to preserve the original formula. However, if you make a mistake overwriting the formula it is not the end of the world. You can “undo” the action by pressing keyboard shortcut keys CTRL + z or pressing the “Undo” button
5.3 Other errors
Floating-point arithmetic may give inaccurate results in Excel - Article
Floating-point errors are usually very small, often beyond the 15th decimal place, and in most cases don't affect calculations significantly.
Functions in 'Logical' category
The BYCOL function function is one of 16 functions in the 'Logical' category.
How to comment
How to add a formula to your comment
<code>Insert your formula here.</code>
Convert less than and larger than signs
Use html character entities instead of less than and larger than signs.
< becomes < and > becomes >
How to add VBA code to your comment
[vb 1="vbnet" language=","]
Put your VBA code here.
[/vb]
How to add a picture to your comment:
Upload picture to postimage.org or imgur
Paste image link to your comment.
Contact Oscar
You can contact me through this contact form