Unique groups of two numbers, all numbers in a unique group

dtaylor646

New Member
Joined
Jun 12, 2018
Messages
8
Office Version
  1. 365
Platform
  1. Windows
Hello everyone,

Though some other posts have had similar solutions, I want to pair a list of unique identifiers to others with each pairing unique for each month. April has #1 with #3 and necessarily #3 with #1 The first three pairings were manual and would like a formula to generate the other pairings for other months, where the pairings do not repeat from the previous months' pairs. Thank you for any and all help!

[TABLE="width: 500"]
<tbody>[TR]
[TD]Identifier[/TD]
[TD]April[/TD]
[TD]May[/TD]
[TD]June [/TD]
[TD]July[/TD]
[/TR]
[TR]
[TD]1[/TD]
[TD]3[/TD]
[TD]2[/TD]
[TD]4[/TD]
[TD][formula here][/TD]
[/TR]
[TR]
[TD]2[/TD]
[TD]4[/TD]
[TD]1[/TD]
[TD]3[/TD]
[TD][/TD]
[/TR]
[TR]
[TD]3[/TD]
[TD]1[/TD]
[TD]4[/TD]
[TD]2[/TD]
[TD][/TD]
[/TR]
[TR]
[TD]4[/TD]
[TD]2[/TD]
[TD]3[/TD]
[TD]1[/TD]
[TD][/TD]
[/TR]
[TR]
[TD]5[/TD]
[TD]7[/TD]
[TD]6[/TD]
[TD]8[/TD]
[TD][/TD]
[/TR]
[TR]
[TD]6[/TD]
[TD]8[/TD]
[TD]5[/TD]
[TD]14[/TD]
[TD][/TD]
[/TR]
[TR]
[TD]7[/TD]
[TD]5[/TD]
[TD]8[/TD]
[TD]20[/TD]
[TD][/TD]
[/TR]
[TR]
[TD]8[/TD]
[TD]6[/TD]
[TD]7[/TD]
[TD]5[/TD]
[TD][/TD]
[/TR]
[TR]
[TD]9[/TD]
[TD]11[/TD]
[TD]10[/TD]
[TD]12[/TD]
[TD][/TD]
[/TR]
[TR]
[TD]10[/TD]
[TD]12[/TD]
[TD]9[/TD]
[TD]11[/TD]
[TD][/TD]
[/TR]
[TR]
[TD]11[/TD]
[TD]9[/TD]
[TD]12[/TD]
[TD]10[/TD]
[TD][/TD]
[/TR]
[TR]
[TD]12[/TD]
[TD]10[/TD]
[TD]11[/TD]
[TD]9[/TD]
[TD][/TD]
[/TR]
[TR]
[TD]13[/TD]
[TD]19[/TD]
[TD]14[/TD]
[TD]15[/TD]
[TD][/TD]
[/TR]
[TR]
[TD]14[/TD]
[TD]20[/TD]
[TD]13[/TD]
[TD]6[/TD]
[TD][/TD]
[/TR]
[TR]
[TD]15[/TD]
[TD]17[/TD]
[TD]16[/TD]
[TD]13[/TD]
[TD][/TD]
[/TR]
[TR]
[TD]16[/TD]
[TD]18[/TD]
[TD]15[/TD]
[TD]17[/TD]
[TD][/TD]
[/TR]
[TR]
[TD]17[/TD]
[TD]15[/TD]
[TD]18[/TD]
[TD]16[/TD]
[TD][/TD]
[/TR]
[TR]
[TD]18[/TD]
[TD]16[/TD]
[TD]17[/TD]
[TD]19[/TD]
[TD][/TD]
[/TR]
[TR]
[TD]19[/TD]
[TD]13[/TD]
[TD]20[/TD]
[TD]18[/TD]
[TD][/TD]
[/TR]
[TR]
[TD]20[/TD]
[TD]14[/TD]
[TD]19[/TD]
[TD]7[/TD]
[TD][/TD]
[/TR]
</tbody>[/TABLE]
 

Excel Facts

Which Excel functions can ignore hidden rows?
The SUBTOTAL and AGGREGATE functions ignore hidden rows. AGGREGATE can also exclude error cells and more.
Welcome to the board.

Provide several examples that illustrate the logic.
 
Upvote 0
Welcome to the board.

Provide several examples that illustrate the logic.


From the above grid, let's say #7 is selected to be paired with identifier #1 in July. In that case...on the seventh line down...identifier #7 must be paired with #1 since that was the number chosen as a pair. So it looks like what I am seeking if a nested formula that generates a 'random' number that is not the identifier or previous pairings (for #1 , not #1 , 3, 2, or 4]...but is recursive that if a number is selected above, the pairing is forced in the cell. The last nuance of logic is that all the numbers need to be uniquely paired. From the above example...if numbers 1-18 are assigned to identifiers #1 -18 in July...this cannot work since #s 19 and #20 were already paired together in May.

Hopefully explaining the restrictions helps clarify the solution I am trying to find.
 
Upvote 0
I don't have a clue what you're trying to do, sorry.
 
Upvote 0
If I read the request right, the OP wants a random list of the numbers 1-20 in the July column, with the proviso that no numbers in any row are duplicated.

If that's correct, I think a formula might be a bit complicated. I have written a macro that should create such a list. Given the layout in post # 1, with the upper right corner being A1, try this:

Open a copy of your workbook. Press Alt-F11 to open the VBA editor. From the menu, select Insert > Module. In the window that opens, paste this:

Code:
Sub NewGroup()
Dim NewCol(1 To 20, 1 To 1) As Long, i As Long, j As Long, dict As Object
Dim Ctr1 As Long, Ctr2 As Long, MyData As Variant, c As Long, LastCol As Long

    Ctr1 = 0
    LastCol = Range("A2").End(xlToRight).Column
    MyData = Range("A2").Resize(20, LastCol).Value
    Set dict = CreateObject("Scripting.Dictionary")
    
BigLoop:
    Erase NewCol
    For c = 1 To 20
        dict.RemoveAll
        Ctr2 = 0
        For i = 1 To 20
            For j = 1 To LastCol
                If MyData(c, j) = i Then GoTo NextI:
            Next j
            For j = 1 To c - 1
                If NewCol(j, 1) = i Then GoTo NextI:
            Next j
            Ctr2 = Ctr2 + 1
            dict.Add Ctr2, i
NextI:
        Next i
        If Ctr2 = 0 Then
            If Ctr1 > 1000 Then
                MsgBox "Could not find a solution after 1000 tries"
                Exit Sub
            End If
            Ctr1 = Ctr1 + 1
            GoTo BigLoop:
        End If
        NewCol(c, 1) = dict(Int(Rnd() * Ctr2) + 1)
    Next c
    
    Range("A2:A21").Offset(, LastCol).Value = NewCol
        
End Sub
Press Alt-Q to close the editor. From Excel, press Alt-F8 to open the macro selector. Select NewGroup and click Run. You may run it again to get another column. Or delete columns B:D to start anew.

The code is rough and probably can be cleaned up some, but it seems to work. Let us know if it's what you need.
 
Upvote 0
Eric,

I tried your code (results in gray area), but i *think* these are not the desired results.
If i understood correctly the macro should generate pairs, that is:
If Id 1 --> 13 then Id 13 --> 1; If Id 2 --> 6 then Id 6 --> 2 and so on (observe columns April, May and June)


[TABLE="class: grid"]
<tbody>[TR]
[TD="bgcolor: #DCE6F1"][/TD]
[TD="bgcolor: #DCE6F1"]
A
[/TD]
[TD="bgcolor: #DCE6F1"]
B
[/TD]
[TD="bgcolor: #DCE6F1"]
C
[/TD]
[TD="bgcolor: #DCE6F1"]
D
[/TD]
[TD="bgcolor: #DCE6F1"]
E
[/TD]
[/TR]
[TR]
[TD="bgcolor: #DCE6F1"]
1
[/TD]
[TD]
Identifier​
[/TD]
[TD]
April​
[/TD]
[TD]
May​
[/TD]
[TD]
June​
[/TD]
[TD]
July​
[/TD]
[/TR]
[TR]
[TD="bgcolor: #DCE6F1"]
2
[/TD]
[TD]
1​
[/TD]
[TD]
3​
[/TD]
[TD]
2​
[/TD]
[TD]
4​
[/TD]
[TD="bgcolor: #D9D9D9"]
13​
[/TD]
[/TR]
[TR]
[TD="bgcolor: #DCE6F1"]
3
[/TD]
[TD]
2​
[/TD]
[TD]
4​
[/TD]
[TD]
1​
[/TD]
[TD]
3​
[/TD]
[TD="bgcolor: #D9D9D9"]
6​
[/TD]
[/TR]
[TR]
[TD="bgcolor: #DCE6F1"]
4
[/TD]
[TD]
3​
[/TD]
[TD]
1​
[/TD]
[TD]
4​
[/TD]
[TD]
2​
[/TD]
[TD="bgcolor: #D9D9D9"]
20​
[/TD]
[/TR]
[TR]
[TD="bgcolor: #DCE6F1"]
5
[/TD]
[TD]
4​
[/TD]
[TD]
2​
[/TD]
[TD]
3​
[/TD]
[TD]
1​
[/TD]
[TD="bgcolor: #D9D9D9"]
15​
[/TD]
[/TR]
[TR]
[TD="bgcolor: #DCE6F1"]
6
[/TD]
[TD]
5​
[/TD]
[TD]
7​
[/TD]
[TD]
6​
[/TD]
[TD]
8​
[/TD]
[TD="bgcolor: #D9D9D9"]
1​
[/TD]
[/TR]
[TR]
[TD="bgcolor: #DCE6F1"]
7
[/TD]
[TD]
6​
[/TD]
[TD]
8​
[/TD]
[TD]
5​
[/TD]
[TD]
14​
[/TD]
[TD="bgcolor: #D9D9D9"]
11​
[/TD]
[/TR]
[TR]
[TD="bgcolor: #DCE6F1"]
8
[/TD]
[TD]
7​
[/TD]
[TD]
5​
[/TD]
[TD]
8​
[/TD]
[TD]
20​
[/TD]
[TD="bgcolor: #D9D9D9"]
3​
[/TD]
[/TR]
[TR]
[TD="bgcolor: #DCE6F1"]
9
[/TD]
[TD]
8​
[/TD]
[TD]
6​
[/TD]
[TD]
7​
[/TD]
[TD]
5​
[/TD]
[TD="bgcolor: #D9D9D9"]
4​
[/TD]
[/TR]
[TR]
[TD="bgcolor: #DCE6F1"]
10
[/TD]
[TD]
9​
[/TD]
[TD]
11​
[/TD]
[TD]
10​
[/TD]
[TD]
12​
[/TD]
[TD="bgcolor: #D9D9D9"]
18​
[/TD]
[/TR]
[TR]
[TD="bgcolor: #DCE6F1"]
11
[/TD]
[TD]
10​
[/TD]
[TD]
12​
[/TD]
[TD]
9​
[/TD]
[TD]
11​
[/TD]
[TD="bgcolor: #D9D9D9"]
7​
[/TD]
[/TR]
[TR]
[TD="bgcolor: #DCE6F1"]
12
[/TD]
[TD]
11​
[/TD]
[TD]
9​
[/TD]
[TD]
12​
[/TD]
[TD]
10​
[/TD]
[TD="bgcolor: #D9D9D9"]
2​
[/TD]
[/TR]
[TR]
[TD="bgcolor: #DCE6F1"]
13
[/TD]
[TD]
12​
[/TD]
[TD]
10​
[/TD]
[TD]
11​
[/TD]
[TD]
9​
[/TD]
[TD="bgcolor: #D9D9D9"]
8​
[/TD]
[/TR]
[TR]
[TD="bgcolor: #DCE6F1"]
14
[/TD]
[TD]
13​
[/TD]
[TD]
19​
[/TD]
[TD]
14​
[/TD]
[TD]
15​
[/TD]
[TD="bgcolor: #D9D9D9"]
10​
[/TD]
[/TR]
[TR]
[TD="bgcolor: #DCE6F1"]
15
[/TD]
[TD]
14​
[/TD]
[TD]
20​
[/TD]
[TD]
13​
[/TD]
[TD]
6​
[/TD]
[TD="bgcolor: #D9D9D9"]
9​
[/TD]
[/TR]
[TR]
[TD="bgcolor: #DCE6F1"]
16
[/TD]
[TD]
15​
[/TD]
[TD]
17​
[/TD]
[TD]
16​
[/TD]
[TD]
13​
[/TD]
[TD="bgcolor: #D9D9D9"]
19​
[/TD]
[/TR]
[TR]
[TD="bgcolor: #DCE6F1"]
17
[/TD]
[TD]
16​
[/TD]
[TD]
18​
[/TD]
[TD]
15​
[/TD]
[TD]
17​
[/TD]
[TD="bgcolor: #D9D9D9"]
14​
[/TD]
[/TR]
[TR]
[TD="bgcolor: #DCE6F1"]
18
[/TD]
[TD]
17​
[/TD]
[TD]
15​
[/TD]
[TD]
18​
[/TD]
[TD]
16​
[/TD]
[TD="bgcolor: #D9D9D9"]
5​
[/TD]
[/TR]
[TR]
[TD="bgcolor: #DCE6F1"]
19
[/TD]
[TD]
18​
[/TD]
[TD]
16​
[/TD]
[TD]
17​
[/TD]
[TD]
19​
[/TD]
[TD="bgcolor: #D9D9D9"]
12​
[/TD]
[/TR]
[TR]
[TD="bgcolor: #DCE6F1"]
20
[/TD]
[TD]
19​
[/TD]
[TD]
13​
[/TD]
[TD]
20​
[/TD]
[TD]
18​
[/TD]
[TD="bgcolor: #D9D9D9"]
16​
[/TD]
[/TR]
[TR]
[TD="bgcolor: #DCE6F1"]
21
[/TD]
[TD]
20​
[/TD]
[TD]
14​
[/TD]
[TD]
19​
[/TD]
[TD]
7​
[/TD]
[TD="bgcolor: #D9D9D9"]
17​
[/TD]
[/TR]
[TR]
[TD="bgcolor: #DCE6F1"]
22
[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
</tbody>[/TABLE]


I tried different formulas without success :eeek:
Tough one!

M.
 
Upvote 0
Hmm, in examining the original examples, I think you're right. I thought it wouldn't be too hard to update my macro, but it's proving tougher than I thought. I'll think upon it a bit more.
 
Upvote 0
This (i think) would be a possible result


[Table="class: grid"][tr][td="bgcolor: #DCE6F1"][/td][td="bgcolor: #DCE6F1"]
A
[/td][td="bgcolor: #DCE6F1"]
B
[/td][td="bgcolor: #DCE6F1"]
C
[/td][td="bgcolor: #DCE6F1"]
D
[/td][td="bgcolor: #DCE6F1"]
E
[/td][/tr]
[tr][td="bgcolor: #DCE6F1"]
1
[/td][td]
Identifier​
[/td][td]
April​
[/td][td]
May​
[/td][td]
June​
[/td][td]
July​
[/td][/tr]

[tr][td="bgcolor: #DCE6F1"]
2
[/td][td]
1​
[/td][td]
3​
[/td][td]
2​
[/td][td]
4​
[/td][td="bgcolor:#DCE6F1"]
18​
[/td][/tr]

[tr][td="bgcolor: #DCE6F1"]
3
[/td][td]
2​
[/td][td]
4​
[/td][td]
1​
[/td][td]
3​
[/td][td="bgcolor:#DCE6F1"]
6​
[/td][/tr]

[tr][td="bgcolor: #DCE6F1"]
4
[/td][td]
3​
[/td][td]
1​
[/td][td]
4​
[/td][td]
2​
[/td][td="bgcolor:#DCE6F1"]
14​
[/td][/tr]

[tr][td="bgcolor: #DCE6F1"]
5
[/td][td]
4​
[/td][td]
2​
[/td][td]
3​
[/td][td]
1​
[/td][td="bgcolor:#DCE6F1"]
8​
[/td][/tr]

[tr][td="bgcolor: #DCE6F1"]
6
[/td][td]
5​
[/td][td]
7​
[/td][td]
6​
[/td][td]
8​
[/td][td="bgcolor:#DCE6F1"]
12​
[/td][/tr]

[tr][td="bgcolor: #DCE6F1"]
7
[/td][td]
6​
[/td][td]
8​
[/td][td]
5​
[/td][td]
14​
[/td][td="bgcolor:#DCE6F1"]
2​
[/td][/tr]

[tr][td="bgcolor: #DCE6F1"]
8
[/td][td]
7​
[/td][td]
5​
[/td][td]
8​
[/td][td]
20​
[/td][td="bgcolor:#DCE6F1"]
10​
[/td][/tr]

[tr][td="bgcolor: #DCE6F1"]
9
[/td][td]
8​
[/td][td]
6​
[/td][td]
7​
[/td][td]
5​
[/td][td="bgcolor:#DCE6F1"]
4​
[/td][/tr]

[tr][td="bgcolor: #DCE6F1"]
10
[/td][td]
9​
[/td][td]
11​
[/td][td]
10​
[/td][td]
12​
[/td][td="bgcolor:#DCE6F1"]
20​
[/td][/tr]

[tr][td="bgcolor: #DCE6F1"]
11
[/td][td]
10​
[/td][td]
12​
[/td][td]
9​
[/td][td]
11​
[/td][td="bgcolor:#DCE6F1"]
7​
[/td][/tr]

[tr][td="bgcolor: #DCE6F1"]
12
[/td][td]
11​
[/td][td]
9​
[/td][td]
12​
[/td][td]
10​
[/td][td="bgcolor:#DCE6F1"]
16​
[/td][/tr]

[tr][td="bgcolor: #DCE6F1"]
13
[/td][td]
12​
[/td][td]
10​
[/td][td]
11​
[/td][td]
9​
[/td][td="bgcolor:#DCE6F1"]
5​
[/td][/tr]

[tr][td="bgcolor: #DCE6F1"]
14
[/td][td]
13​
[/td][td]
19​
[/td][td]
14​
[/td][td]
15​
[/td][td="bgcolor:#DCE6F1"]
17​
[/td][/tr]

[tr][td="bgcolor: #DCE6F1"]
15
[/td][td]
14​
[/td][td]
20​
[/td][td]
13​
[/td][td]
6​
[/td][td="bgcolor:#DCE6F1"]
3​
[/td][/tr]

[tr][td="bgcolor: #DCE6F1"]
16
[/td][td]
15​
[/td][td]
17​
[/td][td]
16​
[/td][td]
13​
[/td][td="bgcolor:#DCE6F1"]
19​
[/td][/tr]

[tr][td="bgcolor: #DCE6F1"]
17
[/td][td]
16​
[/td][td]
18​
[/td][td]
15​
[/td][td]
17​
[/td][td="bgcolor:#DCE6F1"]
11​
[/td][/tr]

[tr][td="bgcolor: #DCE6F1"]
18
[/td][td]
17​
[/td][td]
15​
[/td][td]
18​
[/td][td]
16​
[/td][td="bgcolor:#DCE6F1"]
13​
[/td][/tr]

[tr][td="bgcolor: #DCE6F1"]
19
[/td][td]
18​
[/td][td]
16​
[/td][td]
17​
[/td][td]
19​
[/td][td="bgcolor:#DCE6F1"]
1​
[/td][/tr]

[tr][td="bgcolor: #DCE6F1"]
20
[/td][td]
19​
[/td][td]
13​
[/td][td]
20​
[/td][td]
18​
[/td][td="bgcolor:#DCE6F1"]
15​
[/td][/tr]

[tr][td="bgcolor: #DCE6F1"]
21
[/td][td]
20​
[/td][td]
14​
[/td][td]
19​
[/td][td]
7​
[/td][td="bgcolor:#DCE6F1"]
9​
[/td][/tr]

[tr][td="bgcolor: #DCE6F1"]
22
[/td][td][/td][td][/td][td][/td][td][/td][td][/td][/tr]
[/table]


I achieved this with a formula after many F9 to generate a result without any error ;)
Of course a formula that generates errors and requires many trials is not a proper (decent) solution!

M.
 
Upvote 0
OK, this macro pairs up the random selections:

Code:
Sub NewGroup()
Dim NewCol(1 To 20, 1 To 1) As Long, i As Long, j As Long, MyOptions(1 To 20) As Long
Dim Ctr1 As Long, Ctr2 As Long, MyData As Variant, c As Long, LastCol As Long
Dim UsedRows(0 To 20) As Object, x As Long, y As Long

    Ctr1 = 0
    LastCol = Range("A2").End(xlToRight).Column
    MyData = Range("A2").Resize(20, LastCol).Value
    Set UsedRows(0) = CreateObject("Scripting.Dictionary")
    For i = 1 To 20
        Set UsedRows(i) = CreateObject("Scripting.Dictionary")
        For j = 1 To LastCol
            UsedRows(i).Add CLng(MyData(i, j)), 1
        Next j
    Next i
    
BigLoop:
    Erase NewCol
    UsedRows(0).RemoveAll
    For c = 1 To 20
        If NewCol(c, 1) <> 0 Then GoTo NextC:
        Erase MyOptions
        Ctr2 = 0
        For i = 1 To 20
            If Not UsedRows(c).exists(i) And Not UsedRows(0).exists(i) Then
                Ctr2 = Ctr2 + 1
                MyOptions(Ctr2) = i
            End If
        Next i
        If Ctr2 = 0 Then GoTo ChkCtr2:
        For i = 1 To Ctr2
            x = Int(Rnd() * (Ctr2 + 1 - i) + 1)
            y = MyOptions(x)
            If NewCol(y, 1) = 0 Then
                NewCol(c, 1) = y
                NewCol(y, 1) = c
                UsedRows(0).Add CLng(c), 1
                UsedRows(0).Add CLng(y), 1
                GoTo NextC:
            End If
            MyOptions(i) = MyOptions(Ctr2 + 1 - i)
        Next i

ChkCtr2:
        If Ctr1 > 1000 Then
            MsgBox "Could not find a solution after 1000 tries"
            Exit Sub
        End If
        Ctr1 = Ctr1 + 1
        GoTo BigLoop:
NextC:
    Next c
    
    Range("A2:A21").Offset(, LastCol).Value = NewCol
        
End Sub
It's an iterative solution, but it seems to work fast up to 20 columns.
 
Upvote 0
Eric,

The formula i created requires some F9, not many, to generate correct results.

Array formula in E2 copied down
=IF(COUNTIF(E$1:E1,$A2),INDEX($A$1:$A1,MATCH($A2,E$1:E1,0)),SMALL(IF(ISNA(MATCH($A$2:$A$21,$A2:D2,0)),IF(ISNA(MATCH($A$2:$A$21,E$1:E1,0)),IF(ISNA(MATCH($A$2:$A$21,$A$1:A1,0)),$A$2:$A$21))),RANDBETWEEN(1,MAX(1,20-SUMPRODUCT(1/COUNTIF($A$2:D2,$A$2:D2))-SUM(IF(COUNTIF($A$2:D2,E$1:E1)=0,1))+1))))
Ctrl+Shift+Enter

M.
 
Upvote 0

Forum statistics

Threads
1,223,894
Messages
6,175,254
Members
452,624
Latest member
gregg777

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top