Use Excel to find combinations

faugrad

New Member
Joined
Oct 12, 2010
Messages
30
I have a table with the following data (example):
ChrisFlorida1
JamieTexas1
SamanthaGeorgia1
JohnMaine1
JimWisconsin1
SteveNebraska2
MelanieFlorida2
HectorCalifornia2
SusannaOregon3
DavidKentucky3

<tbody>
</tbody>


What I am trying to do is find a possible function in Excel that finds all possible combinations based on the digits in the last column. It should find all possible combinations except the ones based on the same number and put them next to each other. Example:

ChrisFlorida12NebraskaSteve
ChrisFlorida12FloridaMelanie
ChrisFlorida12CaliforniaHector
ChrisFlorida13OregonSusanna
ChrisFlorida13KentuckyDavid
JamieTexas12NebraskaSteve
JamieTexas12FloridaMelanie
JamieTexas12CaliforniaHector
JamieTexas13OregonSusanna
JamieTexas13KentuckyDavid

<tbody>
</tbody>



And so on until I have all the combinations.
 

Excel Facts

What does custom number format of ;;; mean?
Three semi-colons will hide the value in the cell. Although most people use white font instead.
Hi,

Written in a hurry and untested, but does this help?

Change the sheet names/ranges to match your data.


Excel 2003
ABC
1NamePlaceNumber
2ChrisFlorida1
3JamieTexas1
4SamanthaGeorgia1
5JohnMaine1
6JimWisconsin1
7SteveNebraska2
8MelanieFlorida2
9HectorCalifornia2
10SusannaOregon3
11DavidKentucky3
Sheet1



Code:
Sub CopyStuff()
    Dim ws As Worksheet, ws1 As Worksheet
    Dim rng As Range, rcell As Range
    Dim LR As Long, lResize As Long
    
    Set ws = Sheets("Sheet1")
    Set ws1 = Sheets("Sheet2")
    
    Set rng = ws.Range("A1:C" & ws.Range("A" & ws.Rows.Count).End(xlUp).Row)
    
    For Each rcell In Application.Index(rng, 0, 3)
        If IsNumeric(rcell.Value) Then
            LR = ws1.Range("A" & ws1.Rows.Count).End(xlUp).Row + 1
            With rng
                .AutoFilter Field:=3, Criteria1:="<>" & rcell.Value
                .Offset(1).Columns(3).SpecialCells(xlCellTypeVisible).Copy
                ws1.Range("D" & LR).PasteSpecial xlPasteValues
                .Offset(1).Columns(2).SpecialCells(xlCellTypeVisible).Copy
                ws1.Range("E" & LR).PasteSpecial xlPasteValues
                .Offset(1).Columns(1).SpecialCells(xlCellTypeVisible).Copy
                ws1.Range("F" & LR).PasteSpecial xlPasteValues
                lResize = ws1.Range("D" & ws1.Rows.Count).End(xlUp).Row
                .AutoFilter
                .Range(rcell, rcell.Offset(0, -2)).Copy
                ws1.Range("A" & LR & ":C" & lResize).PasteSpecial xlPasteValues
            End With
        End If
    Next rcell
    
End Sub
 
Upvote 0
Hi,

Written in a hurry and untested, but does this help?

Change the sheet names/ranges to match your data.

Excel 2003
ABC
1NamePlaceNumber
2ChrisFlorida1
3JamieTexas1
4SamanthaGeorgia1
5JohnMaine1
6JimWisconsin1
7SteveNebraska2
8MelanieFlorida2
9HectorCalifornia2
10SusannaOregon3
11DavidKentucky3

<COLGROUP><COL><COL><COL><COL></COLGROUP><THEAD>
</THEAD><TBODY>
</TBODY>
Sheet1




Code:
Sub CopyStuff()
    Dim ws As Worksheet, ws1 As Worksheet
    Dim rng As Range, rcell As Range
    Dim LR As Long, lResize As Long
    
    Set ws = Sheets("Sheet1")
    Set ws1 = Sheets("Sheet2")
    
    Set rng = ws.Range("A1:C" & ws.Range("A" & ws.Rows.Count).End(xlUp).Row)
    
    For Each rcell In Application.Index(rng, 0, 3)
        If IsNumeric(rcell.Value) Then
            LR = ws1.Range("A" & ws1.Rows.Count).End(xlUp).Row + 1
            With rng
                .AutoFilter Field:=3, Criteria1:="<>" & rcell.Value
                .Offset(1).Columns(3).SpecialCells(xlCellTypeVisible).Copy
                ws1.Range("D" & LR).PasteSpecial xlPasteValues
                .Offset(1).Columns(2).SpecialCells(xlCellTypeVisible).Copy
                ws1.Range("E" & LR).PasteSpecial xlPasteValues
                .Offset(1).Columns(1).SpecialCells(xlCellTypeVisible).Copy
                ws1.Range("F" & LR).PasteSpecial xlPasteValues
                lResize = ws1.Range("D" & ws1.Rows.Count).End(xlUp).Row
                .AutoFilter
                .Range(rcell, rcell.Offset(0, -2)).Copy
                ws1.Range("A" & LR & ":C" & lResize).PasteSpecial xlPasteValues
            End With
        End If
    Next rcell
    
End Sub

Thanks a lot! This is very close to what I need. There is one thing I noticed, it seems to take everything twice (mirrored). It does all combinations double like this, which I am trying to avoid:
ChrisFlorida12NebraskaSteve
SteveNebraska21FloridaChris

<TBODY>
</TBODY>
 
Upvote 0

Forum statistics

Threads
1,203,203
Messages
6,054,125
Members
444,703
Latest member
pinkyar23

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