Randomize from set of values
I got similar results to Mike H. We don't lie doing homework assingments here.
Sub MakeList()
Characters = _
Array("C", "F", "G", "H", "J", "K", "M", "N", "P", "Q", "R", _
"S", "T", "W", "X", "Y", "Z")
Digits = _
Array("2", "4", "5", "6", "7", "9")
Numchar = UBound(Characters)
NumDigits = UBound(Digits)
'format column A where results go to text format
Columns("A").NumberFormat = "@"
'initialize the random number generator
Randomize
For RowCount = 1 To 72
'loop until data doesn't match any previous data
Do
Results = ""
For CharCount = 1 To 3
NewChar = Characters(Numchar * Rnd())
Results = Results & NewChar
Next CharCount
For DigitCount = 1 To 3
NewDigit = Digits(NumDigits * Rnd())
Results = Results & NewDigit
Next DigitCount
'check if results already exists
Set c = Columns("A").Find(what:=Results, _
LookIn:=xlValues, lookat:=xlWhole)
If c Is Nothing Then
Range("A" & RowCount) = Results
End If
Loop While Not c Is Nothing
Next RowCount
End Sub
"Mike H" wrote:
Hi,
Try this
Sub marine()
Dim AaRRay As Variant
Dim NaRRay As Variant
Dim r As Long, x As Long
r = 1
AaRRay = Array("C", "F", "G", "H", "J", "K", "M", "N", "P", "Q", "R", "S",
"T", "W", "X", "Y", "Z")
NaRRay = Array("2", "4", "5", "6", "7", "9")
Debug.Print AaRRay(3)
For nums = 1 To 72
Do
For x = 1 To 3
pos = Int((13 - 0 + 1) * Rnd + 0)
mystring = mystring + AaRRay(pos)
Next
For x = 1 To 3
pos = Int((5 - 0 + 1) * Rnd + 0)
mystring = mystring + NaRRay(pos)
Next
Cells(r, 1).Value = mystring
Loop Until WorksheetFunction.CountIf(Range("A1:A" & r), Range("A" & r)) = 1
r = r + 1
mystring = ""
Next
End Sub
mike
"Bam" wrote:
Hi All,
I was given a task of making a unique set of random (72 x 6 digit)
alphanumeric from the following available letters & numbers.
C F G H J K M N P Q R S T W X Y Z
2 4 5 6 7 9
The format is to be like this CFG245.
All 72 Values must be unique.
They must all be 3 alpha & 3 numeric, in that order.
I had a look at zrandom, but couldn't see it doing this task for me.
Has anyone the ability to code something like this in vba?
Or is this simply beyond excel's capabilities??
Any assistance would be much appreciated.
Cheers,
Bam.
|