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))
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 How to use the CHOOSECOLS function How to use the CHOOSEROWS function How to use the DROP […]
Table of Contents Count cells containing text from list Count entries based on date and time Count cells with text […]
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