Summary

To count visible columns in a range, you can use a helper formula based on the CELL function with IF, then tally results with the SUM function. In the example shown, the formula in I4 is:

=SUM(key)

where "key" is the named range B4:F4, and all cells contain this formula, copied across:

=N(CELL("width",B4)>0)

To see the count change, you must force calculation with F9, or perform another worksheet change that triggers recalculation. Below is the same worksheet with all columns visible:

Count visible columns - all columns unhidden

Note: I ran into the core idea for this formula on the excellent wmfexcel.com site.

Generic formula

=N(CELL("width",A1)>0)

Explanation 

There is no direct way to detect a hidden column with a formula in Excel. You might think of using the SUBTOTAL function, but SUBTOTAL only works with vertical ranges. As a result, the approach described in this example is a workaround based on a helper formula that must be entered in a range that includes all columns in the scope of interest. In this example, this range is the named range "key".

In the example shown, columns C and E are hidden. The helper formula, entered in B4 and copied across B4:F4, is based on the CELL function:

=CELL("width",B4)>0

The CELL function will only return a width for a cell in a visible column. When a column is hidden, the same formula will return zero. By checking if the result is greater than zero, we get a TRUE or FALSE result. The N function is used to coerce TRUE to 1 and FALSE to zero, so the final result is 1 when a column is visible, and 0 when a column is hidden. Nice.

To count visible columns, we use the SUM function formula in I4:

=SUM(key)

where "key" is the named range B4:F4.

Count hidden columns

To count hidden columns, the formula in I5 is:

=COLUMNS(key)-SUM(key)

The COLUMNS function returns the total columns in the range (5) and the SUM function returns the sum of visible columns (3), so the final result is 2:

=COLUMNS(key)-SUM(key)
=5-3
=2

With other operations

Once you have the "column key" in place, you can use it with other operations. For example, you could SUM values in visible columns by using SUM like this:

=SUM(key*B6:F6)

Although each cell in B6:F6 contains the number 25, SUM will return 75 when column C and E are hidden, as shown in the example.

Note: CELL function is a volatile function. Volatile functions normally recalculate with every worksheet change, so they can cause performance problems. Unfortunately, CELL does not fire when a column is hidden or made visible again. This means you will not see correct results until the worksheet recalculates, either with a normal change, or by pressing F9.

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.