VBA: Look at column A, Paste all combination pairs into B & C

scola

New Member
Joined
Mar 11, 2014
Messages
5
Hi all (y),

I've had a look on the forum, but cant quite find what I'm looking for. So hopefully you can help!

Here is an example:
ABC
Org1Org1Org2
Org2Org1Org3
Org3Org1Org4
Org4Org1Org5
Org5Org2Org1
Org2Org3
Org2Org4
Org2Org5
Org3Org1
Org3Org2
Org3Org4
Org3Org5
Org4Org1
Org4Org2
Org4Org3
Org4Org5
Org5Org1
Org5Org2
Org5Org3
Org5Org4

<colgroup><col span="3"></colgroup><tbody>
</tbody>

Column A will hold the data sets (will be a variable list).
Column B & C should output all 'bi-directional' pairs that could be created by column A.

Notice that they aren't paired with themselves though - thats the bit I can't find an answer to!

Can someone help me with the VBA for this please :)

Thanks in advance!
Harry
 

Excel Facts

Copy a format multiple times
Select a formatted range. Double-click the Format Painter (left side of Home tab). You can paste formatting multiple times. Esc to stop
Hi, can you confirm for a list of 5, how many pairs you expect returned? Or use a shorter number (e.g. 3) and list all the expected outcomes
 
Upvote 0
Hi JackDanIce,

The above list is the full output.

So for a list of X, I would expect X(X-1) rows

Thanks
 
Upvote 0
Adapted from this link: Create list of all pair combinations

Try:
Code:
Sub Permutations()

    Dim rRng    As Range
    Dim p       As Long
    Dim LR      As Long
    Dim lRow    As Long
    Dim vElements   As Variant
    Dim vResult   As Variant
     
    LR = Cells(Rows.Count, 1).End(xlUp).row
    p = 2
     
    vElements = Application.Index(Application.Transpose(Cells(1, 1).Resize(LR)), 1, 0)
    ReDim vResult(1 To p)
    
    Application.ScreenUpdating = False
    
    PermutationsNP vElements, CInt(p), vResult, lRow, 1
    
    Application.ScreenUpdating = True
    
    Erase vResult
    
End Sub
 
Sub PermutationsNP(ByRef vElements As Variant, ByRef p As Long, ByRef vResult As Variant, ByRef lRow As Long, ByRef iIndex As Long)

    Dim i As Long
    Dim j As Long
    Dim bSkip As Boolean
 
    For i = 1 To UBound(vElements)
        bSkip = False
        For j = 1 To iIndex - 1
            If vResult(j) = vElements(i) Then
                bSkip = True
                Exit For
            End If
        Next j
        If Not bSkip Then
            vResult(iIndex) = vElements(i)
            If iIndex = p Then
                lRow = lRow + 1
                Cells(lRow, 2).Resize(, p) = vResult
            Else
                Call PermutationsNP(vElements, p, vResult, lRow, iIndex + 1)
            End If
        End If
    Next i
    
End Sub
 
Last edited:
Upvote 0

Forum statistics

Threads
1,216,076
Messages
6,128,670
Members
449,463
Latest member
Jojomen56

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