LinkBack Thread Tools Search this Thread Display Modes
Prev Previous Post   Next Post Next
  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 394
Default 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


 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
How do I produce consecutive invoice numbers in Excel? John Z Excel Worksheet Functions 4 December 11th 09 07:03 AM
How do I produce Automatic Quotation Numbers in Excel? flymeoutofhere Excel Discussion (Misc queries) 2 September 25th 06 12:16 PM
Delete Numbers and Produce a List Paul Black Excel Programming 6 October 27th 05 12:26 PM
Non-random numbers generated by excel's data analysis random gener Allie Excel Worksheet Functions 10 September 17th 05 06:19 AM
Non-random numbers generated by excel's data analysis random gener Harlan Grove Excel Discussion (Misc queries) 2 September 13th 05 04:06 PM


All times are GMT +1. The time now is 12:45 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"