S.H.A.D.O.
Well-known Member
- Joined
- Sep 6, 2005
- Messages
- 1,915
Good evening,
I am trying to write a program that outputs 5 random numbers without repetition from 50 numbers.
The number of combinations to be produced is in worksheet "Random Numbers" and in cell "P3".
The code below works as far as the above is concerned.
What I would like to add is to output 2 random numbers without repetition from 9 numbers.
I would like to output these 2 extra numbers after it has produced the 5th number and skipped two cells to the right from the previous combination.
Both these combinations are totally seperate from each other so could possibly have the same numbers in them.
So basically, the program will produce two sets of random numbers without repetition:-
The first being 5 numbers from 50 numbers.
The second being 2 numbers from 9 numbers.
The 5 numbers will be output in cells A1:E1 and the 2 numbers will be output in cells G1:H1.
I am trying to write a program that outputs 5 random numbers without repetition from 50 numbers.
The number of combinations to be produced is in worksheet "Random Numbers" and in cell "P3".
The code below works as far as the above is concerned.
Code:
Sub Random_Numbers_Generator()
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 = 2
nFromLucky = 9
Worksheets("Random Numbers").Select
With ActiveSheet
Range("A1:J65536").Select
Selection.ClearContents
ReDim myMain(1 To nFromMain)
ReDim myLucky(1 To nFromLucky)
' nDrawn = .Range("N3").Value
' nFrom = .Range("O3").Value
nComb = .Range("P3").Value
End With
For j = 1 To nComb
For H = 1 To nFromMain
myMain(H) = H
Next H
For k = 1 To nDrawnMain
Randomize
NewNumber:
Number = Int(nFromMain * Rnd) + 1
If myMain(Number) = "" Then
GoTo NewNumber
Else
Cells(j, k) = myMain(Number)
myMain(Number) = ""
End If
Next k
Next j
Range("N3").Select
Application.DisplayAlerts = True
Application.Calculation = xlCalculationAutomatic
Application.ScreenUpdating = True
End Sub
What I would like to add is to output 2 random numbers without repetition from 9 numbers.
I would like to output these 2 extra numbers after it has produced the 5th number and skipped two cells to the right from the previous combination.
Both these combinations are totally seperate from each other so could possibly have the same numbers in them.
So basically, the program will produce two sets of random numbers without repetition:-
The first being 5 numbers from 50 numbers.
The second being 2 numbers from 9 numbers.
The 5 numbers will be output in cells A1:E1 and the 2 numbers will be output in cells G1:H1.