Pairing non-teammate competitors

Woodwalker

New Member
Joined
Jul 19, 2023
Messages
4
Office Version
  1. 2016
Platform
  1. Windows
Need help please, I like to match-up competitors randomly where they can't face their own teammates and they can only face the opponent once. Data is taken from a table
that's arrange like this (Column A: Competitor, Column B: Team). In another table is the output (Column D: Competitor 1, Column E: Team, Column F: Competitor 2, Column G: Team), also there's no duplicates of any competitor's name in Columns D and F. Any help would appreciated. Thanks. Sorry for bad english.
 
This takes about 15 seconds on my fairly old machine so there may be faster ways, but as I understand it, does what you want.

VBA Code:
Sub Make_Matches()
  Dim d As Object, dMatches As Object
  Dim a As Variant, Bits As Variant
  Dim i As Long, j As Long
  Dim CurrTeam As String, m As String

  Set d = CreateObject("Scripting.Dictionary")
  Set dMatches = CreateObject("Scripting.Dictionary")
  a = Range("A2", Range("B" & Rows.Count).End(xlUp)).Value
  For i = 1 To UBound(a) - 1
    CurrTeam = a(i, 2)
    For j = i + 1 To UBound(a)
      If a(j, 2) <> CurrTeam Then
        d(";" & a(i, 1) & ";" & CurrTeam & ";" & a(j, 1) & ";" & a(j, 2) & ";") = i
      End If
    Next j
  Next i
  Randomize
  Do Until d.Count = 0
    m = d.Keys()(Int(Rnd() * d.Count))
    dMatches.Add m, 1
    Bits = Split(m, ";")
    For j = d.Count To 1 Step -1
      If InStr(1, d.Keys()(j - 1), ";" & Bits(1) & ";") > 0 Or InStr(1, d.Keys()(j - 1), ";" & Bits(3) & ";") > 0 Then d.Remove d.Keys()(j - 1)
    Next j
  Loop
  With Range("D2").Resize(dMatches.Count)
    .Resize(UBound(a), 4).ClearContents
    .Value = Application.Transpose(dMatches.Keys)
    .TextToColumns DataType:=xlDelimited, Semicolon:=True, Space:=False, Other:=False, _
        FieldInfo:=Array(Array(1, 9), Array(2, 1), Array(3, 1), Array(4, 1), Array(5, 1), Array(6, 9))
  End With
End Sub

Here is my sample data and one set of results.

Woodwalker.xlsm
ABCDEFG
1CompetitorTeamCompetitorTeamCompetitorTeam
2Comp 1Team 1Comp 11Team 2Comp 77Team 13
3Comp 2Team 1Comp 53Team 9Comp 65Team 11
4Comp 3Team 1Comp 4Team 1Comp 25Team 5
5Comp 4Team 1Comp 35Team 6Comp 37Team 7
6Comp 5Team 1Comp 6Team 1Comp 76Team 13
7Comp 6Team 1Comp 61Team 11Comp 79Team 13
8Comp 7Team 2Comp 5Team 1Comp 44Team 8
9Comp 8Team 2Comp 14Team 3Comp 23Team 4
10Comp 9Team 2Comp 36Team 6Comp 62Team 11
11Comp 10Team 2Comp 29Team 5Comp 51Team 9
12Comp 11Team 2Comp 26Team 5Comp 42Team 7
13Comp 12Team 2Comp 13Team 3Comp 58Team 10
14Comp 13Team 3Comp 64Team 11Comp 78Team 13
15Comp 14Team 3Comp 1Team 1Comp 56Team 10
16Comp 15Team 3Comp 38Team 7Comp 49Team 9
17Comp 16Team 3Comp 22Team 4Comp 52Team 9
18Comp 17Team 3Comp 10Team 2Comp 19Team 4
19Comp 18Team 3Comp 46Team 8Comp 60Team 10
20Comp 19Team 4Comp 3Team 1Comp 16Team 3
21Comp 20Team 4Comp 30Team 5Comp 67Team 12
22Comp 21Team 4Comp 7Team 2Comp 69Team 12
23Comp 22Team 4Comp 15Team 3Comp 71Team 12
24Comp 23Team 4Comp 45Team 8Comp 66Team 11
25Comp 24Team 4Comp 24Team 4Comp 55Team 10
26Comp 25Team 5Comp 12Team 2Comp 17Team 3
27Comp 26Team 5Comp 54Team 9Comp 75Team 13
28Comp 27Team 5Comp 9Team 2Comp 80Team 13
29Comp 28Team 5Comp 21Team 4Comp 32Team 6
30Comp 29Team 5Comp 39Team 7Comp 47Team 8
31Comp 30Team 5Comp 34Team 6Comp 68Team 12
32Comp 31Team 6Comp 41Team 7Comp 74Team 13
33Comp 32Team 6Comp 27Team 5Comp 33Team 6
34Comp 33Team 6Comp 8Team 2Comp 43Team 8
35Comp 34Team 6Comp 48Team 8Comp 57Team 10
36Comp 35Team 6Comp 50Team 9Comp 72Team 12
37Comp 36Team 6Comp 20Team 4Comp 70Team 12
38Comp 37Team 7Comp 2Team 1Comp 63Team 11
39Comp 38Team 7Comp 18Team 3Comp 40Team 7
40Comp 39Team 7Comp 31Team 6Comp 59Team 10
41Comp 40Team 7Comp 28Team 5Comp 73Team 12
42Comp 41Team 7
43Comp 42Team 7
44Comp 43Team 8
45Comp 44Team 8
46Comp 45Team 8
47Comp 46Team 8
48Comp 47Team 8
49Comp 48Team 8
50Comp 49Team 9
51Comp 50Team 9
52Comp 51Team 9
53Comp 52Team 9
54Comp 53Team 9
55Comp 54Team 9
56Comp 55Team 10
57Comp 56Team 10
58Comp 57Team 10
59Comp 58Team 10
60Comp 59Team 10
61Comp 60Team 10
62Comp 61Team 11
63Comp 62Team 11
64Comp 63Team 11
65Comp 64Team 11
66Comp 65Team 11
67Comp 66Team 11
68Comp 67Team 12
69Comp 68Team 12
70Comp 69Team 12
71Comp 70Team 12
72Comp 71Team 12
73Comp 72Team 12
74Comp 73Team 12
75Comp 74Team 13
76Comp 75Team 13
77Comp 76Team 13
78Comp 77Team 13
79Comp 78Team 13
80Comp 79Team 13
81Comp 80Team 13
Sheet3
 
Upvote 0

Excel Facts

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.

Forum statistics

Threads
1,215,237
Messages
6,123,805
Members
449,127
Latest member
Cyko

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