How to use the COLUMNS function
What is the COLUMNS function?
The COLUMNS function allows you to calculate the number of columns in a cell range or array.
Table of Contents
1. Introduction
What is an array?
An array is a collection of values that can be used in a formula or function. Arrays can be one-dimensional (a list of values) or two-dimensional (a table of values). When you work with arrays in Excel you can perform calculations and operations on multiple values at once which can be very powerful.
In Excel, the semicolon (;) and comma (,) characters are used to separate values in an array. However, the character used depends on your regional settings.
- A comma separates values horizontally in an array. For example: ={1, 2, 3, 4, 5} puts all values in the array in one row distributed across columns as far as needed.
- A semicolon separates values vertically in an array. For example: ={1; 2; 3; 4; 5} puts all values in the array in one column spread across rows as far as needed.
- A comma and a semicolon creates a 2D array that is distributed across both columns and rows. ={2,3;4,5} creates a 2D array that has two columns and two rows. The comma separates values into a column each and a semicolon separates values into rows.
When working with arrays in Excel, it's essential to use the correct separator character to ensure that your formulas work correctly.
What is the difference between the COLUMN function and the COLUMNS function?
The difference between the column function and the columns function in Excel is that the column function returns the COLUMN number of a single cell reference, while the COLUMNS function returns the number of columns in an array or a range of cells.
What other functions performs counting like the COLUMNS function?
COLUMNS counts columns in a cell range.
COUNT counts numbers in a cell range.
COUNTA counts not empty cells in a cell range.
COUNTBLANK counts empty cells in a cell range.
COUNTIF counts cells based on a specific condition.
COUNTIFS counts cells based on specific criteria.
2. Syntax
COLUMNS(array)
array | Required. A cell range for which you want to determine the number of columns. |
3. Example
The example above shows a formula that counts the number of columns in cell range C2:J2. Cell range C2:J2 contains random numbers. The grey numbers below cell range C2:J2 shows the count of columns. Cell C6 displays the formula in cell B6.
When is counting columns useful?
- When you need to reference a range of cells that changes dynamically, counting columns can help. For example, if you have a table with a variable number of columns, you can use the COLUMNS function to count the number of columns and use that value to create a dynamic reference.
- Counting columns can help you create formulas that are flexible and can adapt to changes in your data. For example, if you have a formula that references a range of cells, and you want to be able to easily change the number of columns included in the range, counting columns can make it easier to do so.
- The COLUMNS function is often used to create a sequential list of numbers starting from 1 to n, it utilizes a cell reference that contains both absolute and relative cell references making it automatically expand as you copy the cell to adjacent cells. This makes it easier to display values from an output array to a cell each, in earlier Excel versions. Excel 365 spills dynamic arrays to adjacent cells automatically, the COLUMNS function is not that useful anymore in this context.
The COLUMNS category shows many formulas that use the COLUMNS function to count columns.
Formula in cell B6:
The number of columns in cell range C2:J2 are 8.
4. Count columns in an array
The COLUMNS function also calculates the number of rows in an array.
The array has four columns. An array uses delimiting values to separate columns and rows. They may differ between users based on their regional settings.
The formula above uses comma as a column delimiting character and semicolon as a row delimiting character.
83,89,63,81;
54,68,45,2
It is easier to see that the array actually contains four columns if I put values after a semicolon on a new row.
5. Count columns in a value delimited string
Cell B3 contains a text string with numbers separated by commas and semicolons. The TEXTSPLIT function splits the string into an array.
Formula in cell B6:
The array has columns and rows, the columns function counts the number of columns in the array.
Here's a quick breakdown of the formula:
- B3 is the cell that contains the text string to be split.
- TEXTSPLIT(B3,",",";") splits the text string in cell B3 into an array of sub strings, using both commas (,) and semicolons (;) as delimiters. This means that the text string will be split into separate sub strings wherever a comma or semicolon is found.
- COLUMNS counts the number of columns in the resulting array.
5.1 Explaining formula
Step 1 - Split string into an array
The TEXTSPLIT function splits a string into an array based on delimiting values.
Function syntax: TEXTSPLIT(Input_Text, col_delimiter, [row_delimiter], [Ignore_Empty])
TEXTSPLIT(B3,",",";")
Step 2 - Count columns in the array
COLUMNS(TEXTSPLIT(B3,",",";"))
6. Count columns in multiple cell ranges
This example demonstrates how to count columns in an horizontal array based on different cell ranges.
Formula in cell B12:
The HSTACK function is a new Excel 365 function that stacks cell ranges horizontally.
6.1 Explaining formula
Step 1 - Join cell ranges horizontally
The HSTACK function combines cell ranges or arrays. Joins data to the first blank cell to the right of a cell range or array (horizontal stacking)
Function syntax: HSTACK(array1,[array2],...)
HSTACK(C2:J2,C5:F5,C8:G8)
Step 2 - Count columns
The COLUMNS function calculates the number of columns in a cell range.
Function syntax: COLUMNS(array)
COLUMNS(HSTACK(C2:J2,C5:F5,C8:G8))
7. Count columns in multiple Excel Tables
Formula in cell B8:
7.1 Explaining formula
Step 1 - Join Excel Tables horizontally
The HSTACK function combines cell ranges or arrays. Joins data to the first blank cell to the right of a cell range or array (horizontal stacking)
Function syntax: HSTACK(array1,[array2],...)
HSTACK(Table1, Table2)
Step 2 - Count columns
The COLUMNS function calculates the number of columns in a cell range.
Function syntax: COLUMNS(array)
COLUMNS(HSTACK(Table1, Table2))
8. Function not working
The COLUMNS function returns a
- #NAME? error if you misspell the function or the cell ref is invalid.
This function does not propagate error values in the cells that the cell reference points to.
8.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.
8.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 C2:J2 converted to hard-coded value using the F9 key. The COLUMNS function does not care if the array contains errors meaning it does not propagate errors.
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
8.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.
Useful links
COLUMNS function - Microsoft
COLUMNS function
'COLUMNS' function examples
This post explains how to lookup a value and return multiple values. No array formula required.
Table of Contents Count cells containing text from list Count entries based on date and time Count cells with text […]
This article demonstrates a scoreboard, displayed to the left, that sorts contestants based on total scores and refreshes instantly each […]
Functions in 'Lookup and reference' category
The COLUMNS function function is one of 25 functions in the 'Lookup and reference' 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