where code is the named range B5:B16.
COUNTIF with leading zeros
A common situation where leading zeros do not behave as expected is when functions like COUNTIF, COUNTIFS, SUMIF, SUMIFS, etc. are configured to use numbers with leading zeros. To demonstrate this problem, consider the formula below:
Here the COUNTIF function is set up to count values in B4:B8 that are equal to “01”. We expect a result of 1, but COUNTIF returns 5. Somewhere in the calculation process, the leading zeros get dropped and all cells evaluate to 1. This is clearly not the result we want, and shows a limitation of the COUNTIF function. Similarly, if we apply COUNTIF to the worksheet shown above, we get the incorrect result of 4: The leading zeros in “009875” are stripped, and 9875 is counted 4 times, when the correct result for “009875"is 2. Note: COUNTIF is in a group of 8 functions that share some particular quirks and limitations.
SUMPRODUCT solution
A simple solution to this problem is to use the SUMPRODUCT function like this: Working from the inside out, we are using the following expression as a logical test: Because code is the named range B5:B16, which contains 12 values, the expression yields 12 TRUE/FALSE results in an array like this: The TRUE values in the array correspond to the cells in B5:B16 that contain “009875”. You can see we have TRUE at the fourth cell (B8) and the ninth cell (B13). Next, we use a double negative (–) to coerce the TRUE/FALSE values to 1s and 0s, which creates the following array: This array is delivered directly to the SUMPRODUCT function, which sums the array and returns a final result: This is an example of using Boolean algebra in Excel, and you will see many more advanced formulas use this technique. The nice thing about this approach is that it can be easily extended, as explained below.
Sum quantities
As you might have guessed, if you try to use the SUMIF function to sum the quantities associated with code “009875”, the same problem will occur. The formula below returns 14, when the correct result is 7: The cause of the problem is the same: the leading zeros in “009875” get stripped during the SUMIF calculation, which causes “009875” to be grouped together with “9875”, and SUMIF sums the quantities associated with both codes. One of the nicest things about using SUMPRODUCT to perform conditional counts as we did above, is that we can easily extend the logic to perform conditional sums. In this case, all we need to do is multiply the counting logic by the named range qty (C5:C16) like this: This is the formula used in cell G5 of the worksheet. Since we already know that the expression: results in an array of 1s and 0s, we can simplify the formula like this: Then, evaluating quantities (qty), we get: After the two arrays are multiplied together we have: Notice how the zeros in the first array “cancel out” the irrelevant quantities in the second array. In other words, the exact same logic we used to count code “009875” is used to sum quantities associated with code “009875”. The final result from SUMPRODUCT is 7. SUMPRODUCT is a workhorse function that can solve many tricky problems in Excel. See more examples here. Note: technically the double negative (–) is not needed in the formula to sum quantities above because the math operation of multiplying the two arrays together will automatically coerce the TRUE and FALSE values in the first arrays with 1s and 0s. However, the double negative does no harm and makes the counting and summing formulas easier to compare.
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.