Macros Serial number generation with repeatability comparison base

K0st4din

Well-known Member
Joined
Feb 8, 2012
Messages
501
Office Version
  1. 2016
  2. 2013
  3. 2011
  4. 2010
  5. 2007
Platform
  1. Windows
Hi all, after a lot of searching I managed to find something similar on the site that comes close to what I want to do.


But since I don't understand anything, I can't even modify it for my needs, I am asking for your macro help.
I will attach my excel file.
I want to generate random serial numbers and in different situations with different character lengths ie 12 or 14 or 16 or 18.
In cell I2 I have made a drop down menu to choose how many characters to be. In cell J2 - what type should they be (ie Only uppercase letters and numbers, only numbers or lowercase, uppercase and numbers, or some of my choice of selected characters, etc.).
In cell K2, I record how many Countless combinations to generate according to the choices made that start from/in cell A1 to the end (just an example: be it 3, 34569 or 1 million).
Here it comes (and what I was able to find on the site) is to be able in the second worksheet (with some name given by me) to be able to accumulate a base, with already generated combinations, so that there is never repetition in the new ones generated.
Since I have no idea if the macro can add columns, I have made several of the different types of combinations, be it 12, 14, 16 or 18. If after the first column is filled, the macro can add a new column with the search criteria, then it will make no sense for me to put columns for each type (but I leave this as a last touch, to have your say).
I would be extremely grateful if you could help me.
One more thing, in the link where I found a similar solution, there the user requires to have some hyphens, in my case, I don't need them, that is, I need something like this: for example: ODN81T6DRJJY8S or ODN81T6DRJJY8S49 or 068249317896) i.e. is without any signs.
Thanks in advance to each one!
Test serial numbers.xlsm
ABCDEFGHIJKLMNOP
1? CombinationSelect model letters and numbersHow many numbers you need12ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789
214ABCDEFGHIJKLMNOPQRSTUVWXYZ012345678998725414ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789
316abcdefghijklmnopqrstuvwxyz0123456789
4180123456789
5
6
7
8
9
10
generate numbers
Cells with Data Validation
CellAllowCriteria
I2List=$N$1:$N$5
J2List=$O$1:$O$11



Test serial numbers.xlsm
ABCDEFGHIJKLMNOPQRSTUVWXYZ
1121212121212121214141414141414141616161616161616
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
base generated numbers
 
Wow, this is amazing.
I never imagined it to look so smart and practical!
To be honest, I also thought that somewhere there were name tags, but when I looked, it wasn't written, and I didn't know where to put it (like a range).
Yes, it is definitely much easier now, to be able to just write down the length and type of numbers. I wrote it because it is no problem to go into the macro and change, but somehow it is much nicer to do it only in the table.
Thank you so much for the quick help and wonderful solution.
I can wish you to be alive and healthy and to always help people like me who can't cope with a problem!
 
Upvote 0

Excel Facts

Get help while writing formula
Click the italics "fx" icon to the left of the formula bar to open the Functions Arguments dialog. Help is displayed for each argument.
Hello again,
I want to thank you again for the macro, which is great.
However, there is one small detail that during the conversations we had, I got the impression that I can even generate at one time even 1 million numbers, I decided to try and it gave me an error, even after 150,000 lines here:
VBA Code:
MakeCodes = Application.Transpose(d.Items)

I searched the internet and somewhere they say that this Transponse should not be used because it gave the problems for more lines.
How can we fix this problem?
Thank you in advance!
 
Upvote 0
This is some other site where they mention this problem.

I don't know what to change to keep everything the same but without this problem.
 
Upvote 0
Try changing to:

VBA Code:
Sub Test()

    Dim s As Variant
    Dim c() As String
    Dim L As Long, LastCol As Long, N As Long
    Dim r As Range
    
    N = Range("No").Value
    L = Range("Length").Value
    s = Split(StrConv(Range("Characters").Value, vbUnicode), Chr(0))
    ReDim Preserve s(UBound(s) - 1)
    
    c = MakeCodes(N, L, s)

    On Error Resume Next
    Range("MyCodes").ClearContents
    On Error GoTo 0

    With Range("PutResultsHere").Resize(N)
        .Value = c
        .Name = "MyCodes"
    End With
    
    If Range("SaveToVault").Value Then
        With Worksheets("Vault")
            Set r = .Cells(1, Columns.count).End(xlToLeft)
            With .Columns(r.Column + IIf(Len(r), 1, 0))
                .Cells(2).Resize(N).Value = c
                .AutoFit
                .Cells(1).Value = Now()
                .Cells(1).NumberFormat = "d mmm yyyy hh:mm:ss"
            End With
        End With
    End If
    
End Sub
Function MakeCodes(N As Long, L As Long, s As Variant) As String()
    
    Dim d As Scripting.Dictionary
    Dim tmp As String, Output() As String
    Dim i As Long, count As Long
    
    Set d = New Scripting.Dictionary
    d.CompareMode = vbBinaryCompare
    ReDim Output(1 To N, 1 To 1)
    Randomize
    
    Do Until d.count = N
        tmp = ""
        For i = 1 To L
            tmp = tmp & s(Int((UBound(s) - LBound(s) + 1) * Rnd()))
        Next i
        On Error Resume Next
        d.Add tmp, tmp
        If Err = 0 Then
            count = count + 1
            Output(count, 1) = tmp
        End If
        On Error GoTo 0
    Loop

    MakeCodes = Output
    
End Function
 
Upvote 1
Solution
Hello again,
now everything works as it should and does not give this error.
Apparently, this line really confuses things. For fewer lines there is no problem, but with more something bothers him.
Thank you very much for your quick response.
Be alive and healthy!
 
Upvote 0
Hello
sorry to go back to the topic and the question, but after confirming that everything is OK.
Turns out that wasn't exactly the case.
Why:
After fixing the macro and saying everything was ok, I had forgotten to add Sub CheckVault() - and while I was generating 1 million combinations a day, for days on end, I was checking for yellow duplicates and it kept telling me there weren't any.
Realizing I had forgotten to add CheckVault(), I did so today, and upon enabling the macro to do a new 1 million combinations, it again didn't yellow me if there was any random repeat.
For me, it is unacceptable for there to be a repetition, because I will open myself up to very big problems.
Please look at it one more time, why is it so.
Is there a problem that on one day for example I generate 2 million and save the table and on another day I generate other millions?
I was very worried to see that it doesn't show me the duplicates because if I see them, it's no problem to delete a row or two and everything will be fine.
Thank you very much!
VBA Code:
Sub Testserialnumbers()

    Dim s As Variant
    Dim c() As String
    Dim L As Long, LastCol As Long, N As Long
    Dim r As Range
    
    N = Range("No").Value 'is a B4
    L = Range("Length").Value ' is a B3
    s = Split(StrConv(Range("Characters").Value, vbUnicode), Chr(0)) 'is B2
    ReDim Preserve s(UBound(s) - 1)
    
    c = MakeCodes(N, L, s)

    On Error Resume Next
    Range("MyCodes").ClearContents 'is a B8
    On Error GoTo 0

    With Range("PutResultsHere").Resize(N)
        .Value = c
        .Name = "MyCodes" ' is a B8
    End With
    
    If Range("SaveToVault").Value Then 'is a B5
        With Worksheets("Vault") ' is a sheet Vault
            Set r = .Cells(1, Columns.count).End(xlToLeft)
            With .Columns(r.Column + IIf(Len(r), 1, 0))
                .Cells(2).Resize(N).Value = c
                .AutoFit
                .Cells(1).Value = Now()
                .Cells(1).NumberFormat = "d mmm yyyy hh:mm:ss"
            End With
        End With
    End If
    
End Sub
Function MakeCodes(N As Long, L As Long, s As Variant) As String()
    
    Dim d As Scripting.Dictionary
    Dim tmp As String, Output() As String
    Dim i As Long, count As Long
    
    Set d = New Scripting.Dictionary
    d.CompareMode = vbBinaryCompare
    ReDim Output(1 To N, 1 To 1)
    Randomize
    
    Do Until d.count = N
        tmp = ""
        For i = 1 To L
            tmp = tmp & s(Int((UBound(s) - LBound(s) + 1) * Rnd()))
        Next i
        On Error Resume Next
        d.Add tmp, tmp
        If Err = 0 Then
            count = count + 1
            Output(count, 1) = tmp
        End If
        On Error GoTo 0
    Loop

    MakeCodes = Output
    
End Function
Sub CheckVault()

    Dim d As Scripting.Dictionary
    Dim vIn As Variant
    Dim r As Long, c As Long, count As Long
    Dim Dups As String

    Set d = New Scripting.Dictionary
    d.CompareMode = vbBinaryCompare
    vIn = Worksheets("Vault").Range("A1").CurrentRegion.Value

    For c = 1 To UBound(vIn, 2)
        For r = 1 To UBound(vIn)
            If vIn(r, c) = "" Then Exit For
            On Error Resume Next
            d.Add vIn(r, c), vIn(r, c)
            If Err Then
                Dups = Dups & vIn(r, c) & vbLf
                count = count + 1
            End If
            On Error GoTo 0
        Next r
    Next c

    MsgBox count & " duplicates" & vbLf & vbLf & Dups

End Sub
2024-08-15_133427.jpg
 
Upvote 0
The way the code is currently written ..

- Sub MakeCodes will generate a single batch of codes with no duplicates in that batch. If SaveToVault is TRUE, it will save the batch, with a timestamp, to the "Vault" worksheet. It will not check whether the new batch contains codes already in the Vault.

- Sub CheckVault will check all codes in the Vault for duplicates.

Are you saying that Sub CheckVault is not doing this correctly?

If you wish, we could rewrite the code so that Sub MakeCodes looks at the Vault whilst it is generating a new batch, so that no duplicates ever get printed out or saved to the Vault?

But can I ask why you need to be generating "1 million combinations a day, for days on end"? What's the purpose of doing so?
 
Upvote 0
Hi, it's too long to tell and we'll get off topic. My idea was, just like that, to have absolutely no repeats, no matter how many millions I make, be it in a day or in weeks, months. But since I need a database with these combinations, and the excel file becomes very large in hundreds of megabytes, I therefore make/generate several million every day and then take the necessary number of these generated numbers. But the most important thing remains, even if I have a billion, there should be no repetitions. Thank you very much for your help!
 
Upvote 0
Excuse me, because I was writing on the phone and I forgot to answer.
As I said at the beginning, I forgot to put the macro to do the check when I activate it with the button (I thought it did it automatically - which is my mistake, more likely an oversight).
So - Yes, it works.
I thought there would be no repeatability, because realistically, for example, if I have a choice of AZ-0-1 with 14 combinations, that makes 36 by the 14th, which is almost infinity.
I after several days pressed the button and generated 11 million and then - yesterday, when I pressed the repeatability check button, the check became terribly long, I didn't even wait for it, I even closed the table.
It's all super super cool, if only I could avoid this repetitiveness.
Let me ask this (this is going around in my head) - is it possible that if I generated 2 million today and I save the table and tomorrow I generate 3 million, somehow it can't understand it and figuratively speaking it thinks that it starts a kind of always from the beginning?
Thank you very much!
 
Upvote 0

Forum statistics

Threads
1,224,900
Messages
6,181,634
Members
453,059
Latest member
jkevin

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top