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

Posted on 2011-04-28
Medium Priority
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
  • 2
  • 2
  • 2
  • +2
LVL 22

Expert Comment

ID: 35489153
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

rspahitz earned 1400 total points
ID: 35489172
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.

Assisted Solution

ShivaKK earned 200 total points
ID: 35489180
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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

LVL 23

Assisted Solution

by:Michael Fowler
Michael Fowler earned 200 total points
ID: 35489183
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

sstampf earned 200 total points
ID: 35490138
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

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

Author Comment

ID: 35490204
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

ID: 35490274
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!

Featured Post

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.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

New style of hardware planning for Microsoft Exchange server.
Quickbooks hosting can do wonders to your enterprise but considering the points elaborated in the article which will help you to better analyze the outcomes. So scan your business, its needs and then move to the new world of limitless benefits.
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…

839 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