Produce Random Numbers
On Aug 3, 5:55*pm, "joeu2004" wrote:
"Paul Black" wrote:
I tried to run the program using only say 5 numbers
from 50 and exclude producing the second set of numbers
and it gave me a ...
Run-time error '9'
Subscript out of range
... I managed to get around this by commenting out ...
ReDim myLucky(1 To nFromLucky)
Off-hand, I cannot see any reason why the change would lead to that error or
why the work-around would correct it. *I suspect that there is still a
defect in your code, and it really is not working as you intend it to. *You
have just gotten lucky because of the macro's random nature.
If you can reproduce the "out of range" error, I suggest that you post
__that__ macro (the one that fails) so that we can help you correct the root
cause.
Hi joeu2004,
Thanks for the reply.
I would have answered earlier but the discussion group has only just
been updated.
Anyway, here is the code that produces the error ...
Sub Random()
Dim nDrawnMain As Long
Dim nFromMain As Long
Dim nDrawnLucky As Long
Dim nFromLucky As Long
Dim nComb As Long
Dim myMain() As Variant
Dim myLucky() As Variant
Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual
Application.DisplayAlerts = False
nDrawnMain = 5
nFromMain = 50
nDrawnLucky = 0
nFromLucky = 0
Worksheets("Random Numbers").Select
With ActiveSheet
.Columns("A:K").ClearContents
ReDim myMain(1 To nFromMain)
ReDim myLucky(1 To nFromLucky)
nComb = .Range("N18").Value
End With
Randomize
For j = 1 To nComb
For h = 1 To nFromMain
myMain(h) = h
Next h
n = nFromMain
For k = 1 To nDrawnMain
h = Int(n * Rnd) + 1
Range("B2").Offset(j - 1, k - 1) = myMain(h)
If h < n Then myMain(h) = myMain(n)
n = n - 1
Next k
For h = 1 To nFromLucky
myLucky(h) = h
Next
n = nFromLucky
For k = 1 To nDrawnLucky
h = Int(n * Rnd) + 1
Range("B2").Offset(j - 1, nDrawnMain + k) = myLucky(h)
If h < n Then myLucky(h) = myLucky(n)
n = n - 1
Next
Next j
Range("O18").Select
Application.DisplayAlerts = True
Application.Calculation = xlCalculationAutomatic
Application.ScreenUpdating = True
End Sub
Thanks.
Kind regards,
Paul
|