Search code examples
excelcountindexingexcel-formulaworksheet-function

Using COUNTIFS for a series of values at once


Working a step higher then COUNTIFS, I appose a challenge to write a formula without VBA code. The basic data is combined from 1000s of rows with:

  • Column A: rows with values from 1 to 3
  • Column B: rows with values from 1 to 250.

For this purpose lets say, we are looking at all cells of value "1" in column A, that suit value "5" in column B. To find all matches, we'd use COUNTIFS command.

1   1
2   5
1   5
1   7
1   10
3   45
2   12
1   2
2   1

=COUNTIFS(A1:A9;1;B1:B9;5)

The answer here is 1.

Next thing, the "5" in column B belongs to a group, e.g. group from 1 to 9. What would the best way be, to count all the matches in this example, so that for all "1"'s in column A, we'd have to find all matches with values from 1 to 9 in column B?! In the upper example that would result in "4". The obvious solution is with a series of IF commands, but that's unefficient and it easy to make a mistake, that get's easily overseen.

=COUNTIFS(A1:A9;1;B1:B9;"<="&9)

Works only as the upper limit. If I give the third criteria range and condition as ">="&1 it does not work - returns 0.

Gasper


Solution

  • Where the data is in A1:B9, using a lookup table in D1:E10 with letters A-J in column D and numbers 0 to 9 in column E and the following formula in B11 referencing letters entered in A11 and A12:

    =COUNTIFS(A1:A9,1,B1:B9,">="&VLOOKUP(A11,$D$1:$E$10,2,FALSE),B1:B9,"<="&VLOOKUP(A12,$D$1:$E$10,2,FALSE))

    works, changing the letters in A11 and A12 gives the correct count according to what they correspond to in the looku in D1:E10.