darren do
asked on
Generate number combinations without duplicate pairings
Hi,
I need a VBA code to generate 5 number combinations from 1 to 50 without repeating pairs, order does not matter (ex. combination 1 2 3 4 5, number 1 cannot repeated with numbers 2 3 4 5 in the next combinations)
I need a VBA code to generate 5 number combinations from 1 to 50 without repeating pairs, order does not matter (ex. combination 1 2 3 4 5, number 1 cannot repeated with numbers 2 3 4 5 in the next combinations)
Please show a few examples of what you want.
ASKER
Let's say two combinations 5 8 11 18 20 and 5 7 11 17 21 thats not good because the pair 5 and 11 is repeating I wanna generate combinations without such repeats
Thanks
Thanks
This will generate 50 of them. Change the 50's in line 8 and 15 to generate more or less.
Sub RandomPairs()
Dim colR As New Collection
Dim intNum As Integer
Randomize
Do Until colR.Count = 50
intNum = Int(50 * Rnd) + 1
On Error Resume Next
colR.Add intNum, Str(intNum)
On Error GoTo 0
Loop
For intNum = 1 To 50
Debug.Print colR(intNum)
Next
End Sub
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Fabrice, what advantage does yours have over mine?
Only the fact that it is a function returning the collection.
Note: Your solution wasn't showing up when I posted.
Note: Your solution wasn't showing up when I posted.
ASKER
Martin Liss, thank you for your help.
But how do I get it working?
Thanks
But how do I get it working?
Thanks
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
No offences Martin, but I don't think you should take all credit for this.
No offense taken.