Unique pairs from a single column (VBA)

caybrew

New Member
Joined
Dec 13, 2023
Messages
2
Office Version
  1. 2021
Platform
  1. Windows
Hi all, I have multiple columns (A-C) of data that I am looking to analyze and group in unique pairs before outputting the pairs to new columns. All I need is unique groups of 2.

Capture.PNG33.PNG


I found this awesome code from @Eric W but I cant seem to figure out how to make it loop through all my columns. It only works on the first column. Any ideas? Any help is much appreciated!

VBA Code:
Sub Subsets()
Dim MyNames As Variant, OutCell As Range, MyDic As Object, i As Long

    MyNames = Range(Range("A2"), Range("A2").End(xlDown)).Value
    Set OutCell = Range("C1")
    
    For i = 1 To UBound(MyNames)
        Set MyDic = CreateObject("Scripting.Dictionary")
        MyDic(0) = "Sets of " & i
        Call RecurSubs(MyNames, i, 0, 0, "", MyDic)
        OutCell.Offset(, i - 1).Resize(MyDic.Count).Value = WorksheetFunction.Transpose(MyDic.items)
        Set MyDic = Nothing
    Next i
    
End Sub

Sub RecurSubs(ByRef MyNames, ByRef MaxLevel, ByVal CurLevel, ByVal ix, ByVal str1, ByRef MyDic)
Dim i As Long

    If CurLevel = MaxLevel Then
        MyDic(MyDic.Count) = Left(str1, Len(str1) - 2)
        Exit Sub
    End If
    
    For i = ix + 1 To UBound(MyNames)
        Call RecurSubs(MyNames, MaxLevel, CurLevel + 1, i, str1 & MyNames(i, 1) & ", ", MyDic)
    Next i
    
End Sub
 

Attachments

  • Check.jpg
    Check.jpg
    10.4 KB · Views: 5
  • Capture.PNG33.PNG
    Capture.PNG33.PNG
    10.1 KB · Views: 6
  • Capture.PNG33.PNG
    Capture.PNG33.PNG
    10.1 KB · Views: 8

Excel Facts

How to calculate loan payments in Excel?
Use the PMT function: =PMT(5%/12,60,-25000) is for a $25,000 loan, 5% annual interest, 60 month loan.
Welcome to the Forum!

The answer depends on what you mean by etc etc, which is not clear.

I think you want this:

ABCDEFG
1Input
2DogPigSnakeDog, CatPig, CowSnake, Spider
3CatCowSpiderDog, MoosePig, HorseSnake, Bird
4MooseHorseBirdDog, RatPig, SheepSnake, Mouse
5RatSheepMouseCat, MooseCow, HorseSpider, Bird
6Cat, RatCow, SheepSpider, Mouse
7Moose, RatHorse, SheepBird, Mouse
Sheet1

which you can get with just a couple of quick changes to @Eric W's code:
VBA Code:
Sub Subsets()
    
    Dim MyNames As Variant, OutCell As Range, MyDic As Object, i As Long, j As Long

    For j = 1 To 3
        MyNames = Range(Range("A2").Offset(, j - 1), Range("A2").Offset(, j - 1).End(xlDown)).Value
        Set OutCell = Range("D2").Offset(, j - 1)
        
        For i = 2 To 2
            Set MyDic = CreateObject("Scripting.Dictionary")
            Call RecurSubs(MyNames, i, 0, 0, "", MyDic)
            OutCell.Offset(, i - 1).Resize(MyDic.Count).Value = WorksheetFunction.Transpose(MyDic.items)
            Set MyDic = Nothing
        Next i
    Next j

End Sub

But perhaps you want all 216 permutations?
ABCDEFG
1Input
2DogPigSnakeDog, CatPig, CowSnake, Spider
3CatCowSpiderDog, CatPig, CowSnake, Bird
4MooseHorseBirdDog, CatPig, CowSnake, Mouse
5RatSheepMouseDog, CatPig, CowSpider, Bird
6Dog, CatPig, CowSpider, Mouse
7Dog, CatPig, CowBird, Mouse
8Dog, CatPig, HorseSnake, Spider
9Dog, CatPig, HorseSnake, Bird
10Dog, CatPig, HorseSnake, Mouse
11Dog, CatPig, HorseSpider, Bird
12Dog, CatPig, HorseSpider, Mouse
13Dog, CatPig, HorseBird, Mouse
14Dog, CatPig, SheepSnake, Spider
15Dog, CatPig, SheepSnake, Bird
16etc
Sheet1
 
Upvote 0
Solution

Forum statistics

Threads
1,223,893
Messages
6,175,240
Members
452,621
Latest member
Laura_PinksBTHFT

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