where values is the named range K4:M7 and the rule is applied to all data in B4:I15. Although this is a difficult problem for the human eye, it is exactly the kind of thing Excel does very well. The solution above shows matching data in green, and matching values in blue. The colors are applied automatically with conditional formatting and will update instantly if values change. This requires two separate conditional formatting rules, each with its own formula.
Highlight matching data (green)
This formula is based on the COUNTIF function, which takes two arguments: range and criteria and returns a count of values that match criteria. Normally, range represents the cells being checked. For example, to count cells in the range A1:A10 that are equal to 7, you would configure COUNTIF like this: However, in this case, we want to check each cell in B4:I15 against the 12 separate values in K4:M7. One option is to use COUNTIF with values as criteria and use SUM to add up results: Note: the rule is applied to all cells in B4:I15. The relative reference to B4 will change at each cell. Because we are giving COUNTIF 12 separate numbers to evaluate as criteria, COUNTIF will return 12 counts in an array for each cell in the data. To force a single result, we wrap COUNTIF in the SUM function. Any sum greater than zero means B4 contains a number in values, and any non-zero result will evaluate as TRUE and trigger the rule. However, a simpler option is to reverse the COUNTIF configuration like this: Here, range is values , and B4 is criteria. This gives us a single numeric result – the count of the cell value (B4) in values. As above, any non-zero result means that B4 is a number in K4:M7. And any non-zero result triggers the conditional formatting rule and colors the cell.
Highlight matching values (blue)
The above rule highlights numbers in data that appear in values. We can easily make a rule to highlight cells in values that contain numbers in data. In the example shown, the blue highlighting is created with the following formula: Notice the approach is the same as above. Each cell in values K4:M7 becomes the criteria argument inside COUNTIF while the range is data (B4:I15). As above, COUNTIF returns a single count for each cell in values and any non-zero result triggers the rule.
Count all matching values
To count the total number of cells in data (B4:I15) that match a number in values (K4:M7) you can use a formula like this: To count the total number of cells in values (K4:M7) that match a value in data (B4:I15), you can use a formula like this: Here, because the numbers in values often appear more than once in data, we need to force non-zero counts from COUNTIF to 1 before we add them up, to avoid over counting. We do this by checking if the counts are greater than zero and using a double negative (–) to force the TRUE FALSE results to 1s and 0s. Note: you can also use the SUM function in place of SUMPRODUCT above, but you will need to enter the formula as an array formula with control + shift + enter, unless you are using Excel 365.
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.