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

Posted on 2011-04-28
Last Modified: 2012-05-11
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?
Question by:Thyagaraj03
    LVL 22

    Expert Comment

    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.)
    LVL 22

    Accepted Solution

    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.
    LVL 1

    Assisted Solution

    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
    LVL 23

    Assisted Solution

    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

    LVL 12

    Assisted Solution

    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
    LVL 12

    Expert Comment

    The formula I posted above will work even when your values in column A are not sorted.

    Author Comment

    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!

    Author Comment

    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!

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    What Security Threats Are You Missing?

    Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

    Introduction This Article briefly covers methods of calculating the NPV and IRR variants in Excel as well as the limitations in calculating and interpreting IRR results. Paraphrasing Richard Shockley, author of my favourite finance reference tex…
    This tutorial explains how to create a series of drop-down lists that are dependent upon prior selections to guide (“force”) the user to make the correct selection and reduce data errors within Microsoft Excel. Excel 2010 was used for this tutorial;…
    Learn how to create and modify your own paragraph styles in Microsoft Word. This can be helpful when wanting to make consistently referenced styles throughout a document or template.
    This Micro Tutorial will demonstrate how to use longer labels with horizontal bar charts instead of the vertical column chart.

    794 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    16 Experts available now in Live!

    Get 1:1 Help Now