Originally Posted by
wannabe11
thanks for your answers guys , i've came across an example online and i modified the code a bit . it seems to work fine .
still i dont understand the role of the randomize and how the int function solve the issue since this function only bring whole numbers .
, each one generated being unique.
That macro doesn't guarantee uniqueness
Option Explicit
Const N As Long = 10000
Const strLength As Long = 8
Const strChars As String = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
Sub UniqueStrings()
Dim r As Long
Dim s As String, s1 As String
Dim v As Variant
Dim i As Long
'add first string and a seperation char
s = pvtMakeString(strLength, strChars) & "#"
For i = 2 To N
'make another
s1 = pvtMakeString(strLength, strChars)
'check if already in the string list
Do While InStr(s, s1) > 0
'if it is try another until it's not
s1 = pvtMakeString(strLength, strChars)
Loop
s = s & s1 & "#"
Next i
'split string at the #
v = Split(s, "#")
For i = 0 To N - 1
Cells(i + 1, 1) = v(i)
Next i
End Sub
Private Function pvtMakeString(L As Long, C As String) As String
Dim i As Long
Dim r As Long
Dim s As String
For i = 1 To L
r = Int(Len(strChars) * Rnd + 1)
s = s & Mid(C, r, 1)
Next i
pvtMakeString = s
End Function