how to number rows based on duplicate cell values?

I need to number rows based on duplicate values in a different row. i.e.

  1. Column A (the #) —Column B (The Duplicates)
  2. 1------------------------CC111
  3. 2------------------------CC111
  4. 3------------------------CC111
  5. 1------------------------AE231
  6. 2------------------------AE231
  7. 1------------------------BB456
  8. 1------------------------DD444

Write to A2 formula =COUNTIF($B$1:B2;B2) and drag it down