Hi Ed
You can include a loop which inserts the values into your workbook:
For i = 1 To 52
Cells(i, 1) = myArray(i)
Next
(This uses the same range of values as those in the original code).
Here is the full code:
Private Sub InsertRandomData()
Randomize Time
'Set the number of elements needed. This
'demos uses 52 to simulate cards in a deck.
'Remember that this is a demo ... myArray()
'goes out of scope once this procedure ends.
'To make it persistent, move the Dim statement
'to the form's General Declarations area.
Dim x As Integer
'declare an array
Dim myArray(1 To 52) As Integer
'fill the array with consecutive numbers from 1 to 52
For x = 1 To UBound(myArray)
myArray(x) = x
Next
'randomize the array values
RandomizeArray myArray
For i = 1 To 52
Cells(i, 1) = myArray(i)
Next
End Sub
Private Sub RandomizeArray(ArrayIn As Variant)
Dim x As Long
Dim RandomIndex As Long
Dim tmp As Variant
'only if an array was passed
If VarType(ArrayIn) = vbArray Then
'loop through the array elements
For x = UBound(ArrayIn) To LBound(ArrayIn) Step -1
'select another random array index
RandomIndex = Int((x - LBound(ArrayIn) + 1) * _
Rnd + LBound(ArrayIn))
'and reassign its content to the current array member,
'swapping the current member value to the other spot
tmp = ArrayIn(RandomIndex)
ArrayIn(RandomIndex) = ArrayIn(x)
ArrayIn(x) = tmp
Next
Else
'The passed argument was not an
'array; error handler goes here
End If
End Sub
Hope this helps!
Regards
Jane Pratt
Developer Tools Support
Microsoft UK
This posting is provided "AS IS" with no warranties, and confers no rights.
Use of included script samples are subject to the terms specified at
http://www.microsoft.com/info/cpyright.htm.