Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

In Excel, I'm trying to count the number of rows based on some criteria. In order to know where in the column I need to start counting, I need to search backwards for a specific value starting at some cell.


2    <--- Here I want another column that counted four 2's.
2    <--- That same column will contain the number 2 here, since it counted 2 of them.

Any ideas?

share|improve this question
up vote 2 down vote accepted

One method would involve two additional columns. The first column would check for your condition and keep a running total of the matches. The second column would then look at the first column to only display the last numbers in the running counts. So if this is your data:

    A  B  C
1 | 1
2 | 1
3 | 2
4 | 2
5 | 2
6 | 1
7 | 1

In B1 you would have a special formula since it's the first item:


Then starting in B2 you would have the following formula (which you would fill down the column). This formula will count all the matches for your condition (where the value in column A equals two in this case).


The formula starting at in C1 (again fill down) will look for the last number in the counts established in column B:


Basically this says 'if the number next to me is greater than zero (part of a count) and the number right below is is equal to zero, that's the end of a counting run, and we want to display that number. This gives us:

    A  B  C
1 | 1  0
2 | 1  0
3 | 2  1
4 | 2  2
5 | 2  3  3
6 | 1  0
7 | 1  0

I believe this achieves what you were going for. You could hide column B since all you really want to see is column C.

Hope this helps!

share|improve this answer
Thanks a bunch. Exactly what I'm looking for! I started getting elaborate and having columns that hold state, haha... This seems simpler :-) – jabalsad Mar 23 '12 at 12:53

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .