Quick, clean, and to the point

Count matching values in matching columns

Excel formula: Count matching values in matching columns

To count matching values in matching columns, you can use the SUMPRODUCT function together with the ISNUMBER and MATCH functions. In the example shown, the formula in J6 is:


where data (B5:G14) and headers (B4:G4) are named ranges. The result is 22, since there are 22 values that are either "z" or "c" in columns labeled "A" or "B".


In this example, the goal is to count "z" or "c" values in the named range data, but only when the column header is "A" or "B". The formula used to perform this calculation is based on the SUMPRODUCT function:


Working from the inside out, note that SUMPRODUCT contains a single argument, which is composed of this expression:


This expression is formed from two parts, each representing a logical test. The left part tests column headers, and the right tests values.  The two parts are joined with multiplication (*) because the overall logic is AND, and multiplication corresponds to AND in Boolean algebra.

On the left, the MATCH function is used with the ISNUMBER function to match target columns:

ISNUMBER(MATCH(headers,{"A","B"},0)) // match "A" or "B"

Inside MATCH, notice the arguments are "reversed" to maintain the existing data structure: the header values are used for the lookup_value argument, and the array argument is provided as an array constant that contains the values we are looking for, "A" and "B". The result from MATCH is an array composed of #N/A errors or numbers. The numbers indicate matched positions:


There are 6 items in this array because we are testing 6 columns. The numbers represent matched columns and errors represent columns that do not match. This array is returned and handed off to the ISNUMBER function:

ISNUMBER({1,2,#N/A,1,2,#N/A}) // convert to TRUE or FALSE

which returns an array like this:


Note the TRUE values correspond to columns that are either "A" or "B". This completes the column matching logic.

On the right side of the expression, we have similar logic to test the values themselves:


The MATCH function is again used to check for two values "z" or "c" with the same reversed argument approach. Because the named range data contains 60 values, the result from MATCH is an array with 60 values:


The ISNUMBER function again translates this array into TRUE and FALSE values:


Now the original expression above (inside SUMPRODUCT) can be written like this:


Note the multiplication operator is still there, just after the first array. In Excel any math operation will automatically convert TRUE and FALSE values to their numeric equivalents, 1 and 0. This means you can think of the expression like this:


After the expression is evaluated, we have a single array like this:


This array is delivered to the SUMPRODUCT function as the array1 argument. Then, with only one array to process, SUMPRODUCT sums the items in the array and returns a final result: 22.

Note: although SUMPRODUCT can handle multiple arrays as separate arguments, you will see many formulas that place all logic into a single argument. Doing so takes advantage of the fact that Excel automatically coerces TRUE and FALSE values to 1s and 0s during any math operation. When the logic is separated into separate arrays, an additional step must be taken to convert to 1s and 0s. For more details, see Why SUMPRODUCT?

Contains logic

In the example shown above testing logic is "equal to". The columns must be equal to "A" or "B" and the values must be equal to "z" or "c". But sometimes you need to test with "contains" logic. For example, test for values that contain "z" or contain "c".

One consequence of reversing the arguments inside the MATCH function is that wildcards can't be used with the lookup values, because these values appear as the array argument. If you need to test values using contains logic, you can switch to another approach based on ISNUMBER with the SEARCH function. For example, to match values that contain "x" or "c", you can use an expression like this:


Note we are joining each test with the addition operator (+) because in Boolean algebra addition corresponds to OR logic. The final formula would then look like this:


Note an additional set of parentheses () have been added to control order of operations.

Dave Bruns

Excel Formula Training

Formulas are the key to getting things done in Excel. In this accelerated training, you'll learn how to use formulas to manipulate text, work with dates and times, lookup values with VLOOKUP and INDEX & MATCH, count and sum with criteria, dynamically rank values, and create dynamic ranges. You'll also learn how to troubleshoot, trace errors, and fix problems. Instant access. See details here.

Download 100+ Important Excel Functions

Get over 100 Excel Functions you should know in one handy PDF.