Solved

Sequential Unique ID Number

Posted on 2013-10-23
8
299 Views
Last Modified: 2013-11-03
I had assistance writting the following code:

Sub Fill_ID()
Dim LastRow As Long, MaxN As Long, StoreN As String
Dim Rng As Range, A() As String, i As Integer
Worksheets("TEMPLATE").Activate
'Find ID column
For i = 1 To 50
    If Cells(1, i).Value = "TrackerNo" Then Exit For
Next i
If i = 50 Then
    MsgBox "TrackerNo column not found"
    Exit Sub
End If
ColN = i
LastRow = Cells(65536, ColN).End(xlUp).Row ' In Excel 2007 and later last row could be more then 65536
StoreN = Right(Cells(2, 1).Value, 3)
If LastRow = 1 Then
    MaxN = 1
Else
    A = Split(Cells(LastRow, ColN).Value, "-")
    StoreN = Right(Cells(LastRow, ColN).Value, 3)
    MaxN = A(0)
End If
MaxN = MaxN + 0
Do While Cells(LastRow + 1, 6).Value <> ""  'Fill cells where Column F is not empty
    StoreN = Right(Cells(LastRow + 1, 1).Value, 3) 'Get Store Number from column 1
    If MaxN < 999 Then                      
        Cells(LastRow + 1, ColN).Value = StoreN & "-" & Format(MaxN, "000")
        LastRow = LastRow + 1
        MaxN = MaxN + 1
    Else
        Exit Do
    End If
Loop
End Sub

Open in new window


Need additional help just noticed when additional rows of data are added the TrackerNo is not updating correctly.  If possible I would like to have the TrackerNo update sequentially.  Attaching sample you will see that the number jumps from 789-012 to
789-789 instead of 789-013.
EE-TrackerNo.xlsm
0
Comment
Question by:jmac001
  • 3
  • 2
8 Comments
 
LVL 29

Accepted Solution

by:
gowflow earned 250 total points
ID: 39594696
What is the rule ? Just sequentially regardless of the data that is there ? as looking at the logic I see that it looks at the storenumber to build the sequence

So what do you want ?
gowflow
0
 
LVL 20

Assisted Solution

by:CompProbSolv
CompProbSolv earned 250 total points
ID: 39594717
In Line 17 change:
MaxN = A(0)
to:
MaxN=StoreN + 1

and I think you will have it.
0
 

Author Comment

by:jmac001
ID: 39594772
Thank you that resolved the issue.
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 29

Expert Comment

by:gowflow
ID: 39594792
Not so quick !!!
in appearance it give you the correct figure but the code should be

to change this line
MaxN = A(0)

into this line
MaxN = A(1) + 1

as it is taking the second part of the last sequential and adding 1 whereas by changing it to variable StoreN you may not always hit it correctly.

gowflow
0
 
LVL 29

Expert Comment

by:gowflow
ID: 39599983
Looking at this again you can have both solutions
MaxN = A(1) + 1

or
MaxN=StoreN + 1

gowflow
0
 
LVL 20

Expert Comment

by:CompProbSolv
ID: 39607382
Actually, goflow's solution is better.  His uses the characters after the "-" delimiter, regardless of how many there are.  My solution relied on the StoreN calculation, which assumes that the last three characters exactly contain the number.

To see the difference, change P10 from 789-009 to 789-09, delete P11 to the end, and run the macro.  With my solution you will get some odd results (double hyphens).  With goflow's solution you will get what you expect.

My solution works properly with proper data.  Goflow's is somewhat more fault-tolerant (at least for a missing digit).  I've not considered all of the other fault conditions  (e.g. non-numeric characters) and how the two solutions would differ.

I would suggest splitting the points.
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

A little background as to how I came to I design this code: Around 5 years ago I designed an add-in that formatted Excel files to a corporate standard, applying different cell colours and font type depending on whether the cells contained inputs,…
Sparklines have been introduced with Excel 2010 and are a useful tool for creating small in-cell charts, used for example in dashboards. Excel 2010 offers three different types of Sparklines: Line, Column and Win/Loss. What it does not offer is a…
The view will learn how to download and install SIMTOOLS and FORMLIST into Excel, how to use SIMTOOLS to generate a Monte Carlo simulation of 30 sales calls, and how to calculate the conditional probability based on the results of the Monte Carlo …
Graphs within dashboards are meant to be dynamic, representing data from a period of time that will change each time the dashboard is updated with new data. Rather than update each graph to point to a different set within a static set of data, t…

744 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

10 Experts available now in Live!

Get 1:1 Help Now