I have data that looks like this:

Monkey
Donkey
Elephant
Panda
Donkey
Donkey
Monkey

I want to differentiate the rows by a number, counting up for every duplicate:

Monkey      1
Donkey      1
Elephant    1
Panda       1
Donkey      2
Donkey      3
Monkey      2

I have managed to get the amount of duplicates for the rows, but I don't know how to go about enumerating them like this.

Is it even possible with excel, or will I have to write a script?

有帮助吗?

解决方案

Assume that the list is in column A, starting at row 1. In cell B1 you can enter:

=COUNTIF(A1,A1)

Which will of course give 1. (Or you can just enter a hard-coded value of 1, it's up to you.)

In cell B2, enter the formula

=COUNTIF($A$1:A2,A2)

and copy that down for the rest of your list. NOTE CAREFULLY the fact that the cell at the start of the list ($A$1) is absolute, and the end of the list (in this case A2, though that will change as you copy the formula down the rows) is relative. What you're essentially doing is counting up the number of matching items from the start of the list, down to and including that row. So cell B5 will return 2, since there are 2 Donkeys between $A$1 and A5. Cell B6 will return 3, since there are 3 Donkeys between $A$1 and A6, and so on.

其他提示

Use this formula,

=IFERROR(LOOKUP(2,1/(A$1:A1=A2),B$1:B1)+1,1)
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top