| A | B | C | D | E | F |
1 | 5 | 10 | | | | |
2 | 5 | 20 | | | | |
3 | 5 | 30 | | | | |
4 | 10 | 20 | | | | |
5 | 10 | 40 | | | | |
6 | 20 | 30 | | | | |
7 | 20 | 50 | | | | |
8 | 30 | 60 | | | | |
9 | 40 | 50 | | | | |
10 | 40 | 70 | | | | |
11 | 50 | 60 | | | | |
12 | 50 | 80 | | | | |
13 | 60 | 90 | | | | |
14 | 70 | 80 | | | | |
15 | 70 | 100 | | | | |
16 | 80 | 90 | | | | |
17 | 80 | 110 | | | | |
18 | 90 | 120 | | | | |
19 | 100 | 110 | | | | |
20 | 100 | 130 | | | | |
21 | 110 | 120 | | | | |
22 | 110 | 140 | | | | |
23 | 120 | 150 | | | | |
24 | 130 | 140 | | | | |
25 | 130 | 160 | | | | |
26 | 140 | 150 | | | | |
27 | 140 | 170 | | | | |
28 | 150 | 180 | | | | |
29 | 160 | 170 | | | | |
30 | 160 | 190 | | | | |
31 | 170 | 180 | | | | |
32 | 170 | 200 | | | | |
33 | 180 | 210 | | | | |
34 | 190 | 200 | | | | |
35 | 190 | 220 | | | | |
36 | 200 | 210 | | | | |
37 | 200 | 230 | | | | |
38 | 210 | 240 | | | | |
39 | 220 | 230 | | | | |
40 | 220 | 250 | | | | |
41 | 230 | 240 | | | | |
42 | 230 | 260 | | | | |
43 | 240 | 270 | | | | |
44 | 250 | 260 | | | | |
45 | 250 | 280 | | | | |
46 | 260 | 270 | | | | |
47 | 260 | 290 | | | | |
48 | 270 | 300 | | | | |
49 | 280 | 290 | | | | |
50 | 280 | 310 | | | | |
51 | 290 | 300 | | | | |
52 | 290 | 320 | | | | |
53 | 300 | 330 | | | | |
54 | 310 | 320 | | | | |
55 | 310 | 340 | | | | |
56 | 320 | 340 | | | | |
57 | 320 | 330 | | | | |
58 | 330 | 360 | | | | |
59 | | | | | | |
60 | | | | | | |
62 | | | | | | |
| | | | | |
It would be excellent if the result could be delimitated with a comma and each number in the pair separated by a forward slash. /
Are the available pairs always in columns A & B (one number in each cell)?
Will there always be 58 pairs?
Option Explicit
Sub UniqueItemPairs()
' selects 9 pairs randomly from the range A2:B59,
' testing each as it is selected to ensure that neither member of the pair is re-used
Dim TotalPairs As Integer 'this can be changed if there are ever more or fewer pairs
Dim UniquePairs As Integer '
Dim PairCount As Integer
Dim PairCheck As Variant ' this will be the dictionary object to ensure there aren't any duplicated entries
Dim MyPairs As String ' this will hold the output pairs
Dim Checked As Integer
Dim RowNum As Long, NextRow As Long
Dim P1 As String, P2 As String
Set PairCheck = CreateObject("scripting.dictionary") 'this is how we check for uniqueness
TotalPairs = 58 'we use this to establish the range, and to ensure that the macro doesn't go on forever
UniquePairs = 9
PairCount = 1
NextRow = ActiveSheet.Cells(Rows.count, 4).End(xlUp).Row + 1 'column D will hold the output
Randomize
While PairCount <= UniquePairs And Checked < TotalPairs
RowNum = Int(Rnd() * TotalPairs) + 2 'change this to 1 if there's no header row
' MsgBox RowNum 'uncomment for troubleshooting
With ActiveSheet
P1 = .Range("A" & RowNum).Value
P2 = .Range("B" & RowNum).Value
End With
'Now test each member of the pair against the dictionary
If Not PairCheck.Exists(P1) And Not PairCheck.Exists(P2) Then
'first add each item of the pair to the "used" list
PairCheck.Add P1, P1
PairCheck.Add P2, P2
'now add the items as a pair to the output string
MyPairs = MyPairs & P1 & "/" & P2
If PairCount < UniquePairs Then MyPairs = MyPairs & ", "
PairCount = PairCount + 1
End If
'whether or not we found a pair with unique values, increase Checked by 1 so we don't go on forever
Checked = Checked + 1
Wend
' Get rid of the final comma and space
ActiveSheet.Range("D" & NextRow).Value = MyPairs
End Sub
Sub UniqueItemPairs()
' For a user-entered number of rows, columns, and starting position:
' selects 9 pairs randomly from the range A2:B59,
' tests each as it is selected to ensure that neither member of the pair is re-used
' outputs the set of 9 pairs to the next row of a user-selected range
Dim TotalPairs As Integer 'this can be changed below if there are ever more or fewer pairs
Dim UniquePairs As Integer 'this can be changed below if you want more or fewer pairs in the output
Dim PairCount As Integer 'counter to keep track of how many have been collected so far
Dim PairCheck As Variant ' this will be the dictionary object to ensure there aren't any duplicated entries
Dim MyPairs As String ' this will hold the output pairs
Dim Checked As Integer
Dim RowNum As Long, NextRow As Long
Dim P1 As String, P2 As String
Dim HowManyRows As Long, OutputRow As Long
Dim HowManyCols As Long, StartingCol As Long, OutputCol As Long, LastCol As Long
Set PairCheck = CreateObject("scripting.dictionary")
TotalPairs = 58 'we use this to establish the range to search for pairs, and to ensure that the macro doesn't go on forever
UniquePairs = 9
PairCount = 1
HowManyRows = InputBox("How many rows of output do you want?")
If Not HowManyRows > 0 And Not HowManyRows <= 100000 Then
MsgBox "Only enter numbers between 1 and 100000"
Exit Sub
End If
HowManyCols = InputBox("How many columns of output do you want?")
If Not HowManyCols > 0 And Not HowManyCols <= 100 Then
MsgBox "Only enter numbers between 1 and 100"
Exit Sub
End If
StartingCol = InputBox("What column should the output start in? Enter a number between 4 and 100")
If Not StartingCol >= 4 And Not StartingCol <= 100 Then
MsgBox "Only enter numbers between 4 and 100"
Exit Sub
End If
LastCol = StartingCol + ((HowManyCols - 1) * 2)
Randomize
For OutputCol = StartingCol To LastCol Step 2
For OutputRow = 2 To HowManyRows + 1 'allows for header row
'Start with an empty string, and internal counters reset to 1, and empty dictionary object
PairCheck.RemoveAll
PairCount = 1
Checked = 1
MyPairs = ""
While PairCount <= UniquePairs And Checked < TotalPairs
RowNum = Int(Rnd() * TotalPairs) + 2 'change this to 1 if there's no header row
' MsgBox RowNum 'uncomment for troubleshooting
With ActiveSheet
P1 = .Range("A" & RowNum).Value
P2 = .Range("B" & RowNum).Value
End With
'Now test each member of the pair against the dictionary
If Not PairCheck.Exists(P1) And Not PairCheck.Exists(P2) Then
'first add each item of the pair to the "used" list
PairCheck.Add P1, P1
PairCheck.Add P2, P2
'now add the items as a pair to the output string
MyPairs = MyPairs & P1 & "/" & P2
If PairCount < UniquePairs Then MyPairs = MyPairs & ", "
PairCount = PairCount + 1
End If
'whether or not we found a pair with unique values, increase Checked by 1 so we don't go on forever
Checked = Checked + 1
ActiveSheet.Cells(OutputRow, OutputCol).Value = MyPairs
Wend
Next OutputRow
Next OutputCol
End Sub
but it is unlikely that all sets of 9 are unique within that "population"