Solved

# Formula to count the number of times a number appears

Posted on 2013-09-16
Medium Priority
553 Views
I have a spreadsheet and need help on a formula. In column A, there are lots of random numbers, many of which repeat. In column B, I am trying to figure out a formula that will count how many different numbers are there. COUNTIF won't work. Please see the attached spreadsheet as an example. Its hard to explain, the spreadsheet will make more sense.
Example.xlsx
0
Question by:brasiman

LVL 24

Assisted Solution

Steve earned 200 total points
ID: 39497028
If you could try the following in B2 copied down:

=IF(COUNTIF(A\$1:A1,A2)=0,MAX(B\$1:B1)+1,INDEX(B\$1:B1,MATCH(A2,A\$1:A1,0)))
Example.xlsx
0

LVL 81

Expert Comment

ID: 39497157
A somewhat shorter formula that you can use in cell B2 and copy down is:
=IFERROR(VLOOKUP(A2,A\$1:B1,2,FALSE),MAX(B\$1:B1)+1)

If your real goal is to count how many different numbers are in column A, then consider:
=SUMPRODUCT(1/COUNTIF(A2:A15,A2:A15))
This formula requires that range A2:A15 not have any blank cells.
0

LVL 14

Expert Comment

ID: 39500815
I think you might be looking to create a histogram.  Please see if the following tutorial suits your needs.

http://office.microsoft.com/en-us/excel-help/present-your-data-in-a-histogram-HA010342785.aspx
0

Author Comment

ID: 39504949
Thanks for the quick answers, both byundt and the_barman worked great. As i started typing the formula in though. They added one column on me, and its a unique identifier. See the example and it will probably make more sense. I basically need the same thing, but i need the count to start over on every unique ID.
Example.xlsx
0

LVL 81

Expert Comment

ID: 39505836
If you have Excel 2010 or later, you can use the AGGREGATE function:
Put the following formula in C3 (may be copied down):
=IFERROR(LOOKUP(999,C\$2:C2/((A\$2:A2=A3)*(B\$2:B2=B3))),AGGREGATE(14,6,(A\$2:A2=A3)*(C\$2:C2),1)+1)

Alternatively, you can array-enter the following formula in C3:
=IFERROR(LOOKUP(999,C\$2:C2/((A\$2:A2=A3)*(B\$2:B2=B3))),MAX((A\$2:A2=A3)*(C\$2:C2))+1)
To array-enter a formula, click in the formula bar, hold Control and Shift keys down, then hit Enter.

With either approach, put a 1 in cell C2.
ExampleQ28240901.xlsx
0

Author Comment

ID: 39506950
Thanks byundt. I tried it and i'm getting the #NAME? error. i checked the formula and its correct. I tried your alternative approach. i am using excel 2003. any thoughts/ideas?
0

LVL 81

Accepted Solution

byundt earned 1800 total points
ID: 39507002
brasiman,
The IFERROR function requires Excel 2007 or later. That's why you are getting #NAME? error.

For Excel 2003 and earlier, you can copy down a longer array-entered formula in cell C3:
=IF(ISERROR(LOOKUP(999,C\$2:C2/((A\$2:A2=A3)*(B\$2:B2=B3)))),MAX((A\$2:A2=A3)*(C\$2:C2))+1,LOOKUP(999,C\$2:C2/((A\$2:A2=A3)*(B\$2:B2=B3))))

To array-enter a formula, click in the formula bar, hold Control and Shift keys down, then hit Enter. If you see #VALUE! errors when you copy the formula down, then you didn't array-enter the formula.

For future reference, it is always good to specify your software version when you ask an Excel question on Experts Exchange. I've got six different versions of Excel installed on my laptop so I can reproduce what you are seeing and respond appropriately for your version of Excel. Many Askers use the Tags to list their software version.  :-)

ExampleQ28240901.xls
0

Author Comment

ID: 39507108
Brad, you're awesome, thank you! That worked. Good point on specifying the version. Makes sense. My bad on that one. Next time i'll make sure I do that. Thanks again for your help!
0

Author Closing Comment

ID: 39507111
Great suggestions and quick responses, thank you!
0

## Featured Post

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.