MS Excel assing serial number based on the cells matching the contents

I don't know much of MS-Excel. I have an .xls file of 3000 rows/columns. I have to assign serial numbers starting from 0 based on the cells which has same contents. I can do entering 0 in one cell and use drag and drop but doing this for huge file takes much time. The file contents are as below:

A1                B2

Dentist           0
Dentist           1
Dentist           2
Dentist           4
Tax Return        0
Tax Return        1
Tax Return        2
Tax Return        3
Tax Return        4
Tax Return        5
Finance           0
Insurance         0
Insurance         1
something         0
--------          1

Open in new window

In the column B2 I manually did drag and drop. Anybody has any trick so that I can do it for the entire file with a single shot or with less steps?
Who is Participating?
rspahitzConnect With a Mentor Commented:
Sorry....I see that's not what you're asking.
You want to assign a sequence for each category?

How about this formula, starting in cell B3:

=if(A3=A2, B2+1, 0)

Then copy it down.
There are several options.
If you know how many cells you want to go, you can use the Fill Series wizard.

In Excel 2007/2010, go to the Home tab, Editing group, Fill item (box with blue arrow pointing down) and select Series...
(Before you do this, go to the cell where you want it to start, and put a starting number.)
From there, select:
Series in "Columns"
Type "Linear"
Step Value "1"
Stop Value "5000" (or whereever it will end.)
ShivaKKConnect With a Mentor Commented:
You can use the following sub-routine to do it. Open the excel & press Alt + F11. Paste this code in 'ThisWorkbook' and press F5 key. Hope it helps.

Sub Serial()
Dim AValue As String
Dim BValue As Integer
Dim RowCntr As Integer

RowCntr = 1
BValue = 0

AValue = Range("A1").Value

While Range("A" & Trim(Str(RowCntr))).Value <> ""
    Range("B" & Trim(Str(RowCntr))).Value = BValue
    If Range("A" & Trim(Str(RowCntr))).Value <> Range("A" & Trim(Str(RowCntr + 1))).Value Then
        BValue = 0
        BValue = BValue + 1
    End If
    RowCntr = RowCntr + 1

End Sub
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Michael FowlerConnect With a Mentor Solutions ConsultantCommented:
I have attached an example workbook with a macro to do this.

It assumes that like your example the list is sorted and that the data starts on row 3

To put the code into your workbook select ALT+F11, right click on the left and select Insert > Module and then copy and paste the code into the module

Option Explicit

Sub serials()
   Dim lastrow As Long, i As Long, serial As Long
   lastrow = Range("A" & Rows.Count).End(xlUp).Row
   For i = 3 To lastrow
      If Range("A" & i).Value <> Range("A" & i - 1).Value Then serial = 0
      Range("B" & i).Value = serial
      serial = serial + 1

End Sub

Open in new window

sstampfConnect With a Mentor Commented:
This can Very Easily be done though a simple formula. In cell "B3" put this formula:
and drag it down. Let me know if you have any questions
The formula I posted above will work even when your values in column A are not sorted.
Thyagaraj03Author Commented:
Probably I confused many between count and serial number. And the macros will always do in one shot but I'm failing to use them which are posted here as they are giving some errors when I try to run but are useful. The count is also useful for me. Thank you all!
Thyagaraj03Author Commented:
Probably I didn't assign points proper having got the solution though but it's my sole responsibility assigning points equally as much as possible. Everyone's solutions is correct but I couldn't make use of it properly(macros and count). Excuse me!. Thanks a lot!
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.

All Courses

From novice to tech pro — start learning today.