List combinations of numbers

tiredofit

Well-known Member
Joined
Apr 11, 2013
Messages
1,907
Office Version
  1. 365
  2. 2019
Platform
  1. Windows
Assume I have four numbers in a single row, 10, 20 ,30 ,40.

I would like to list off the possible combinations, ie 4C1, 4C2, 4C3, 4C4, where C is the Excel function Combin. Mathematically nCr = n!/[(n-r)!r!]

4C1 returns 4 results, 4C2 returns 6, 4C3 returns 4 and 4C4 returns 1.

The results should be:

Code:
10
20
30
40

10,20
10,30
10,40
20,30
20,40
30,40

10,20,30
10,20,40
10,30,40
20,30,40

10,20,30,40

How can this be done in VBA?

Thanks
 

Excel Facts

Wildcard in VLOOKUP
Use =VLOOKUP("Apple*" to find apple, Apple, or applesauce
Here's a formula option in 365. Let us know if you still want VBA. Try:
Book1
ABC
11010
22020
33010, 20
44030
510, 30
620, 30
710, 20, 30
840
910, 40
1020, 40
1110, 20, 40
1230, 40
1310, 30, 40
1420, 30, 40
1510, 20, 30, 40
Sheet6
Cell Formulas
RangeFormula
C1:C15C1=DROP(MID(REDUCE(0,A1:A4,LAMBDA(a,c,VSTACK(a,a&", "&c))),4,9^9),1)
Dynamic array formulas.
 
Upvote 0
Here's a formula option in 365. Let us know if you still want VBA. Try:
Book1
ABC
11010
22020
33010, 20
44030
510, 30
620, 30
710, 20, 30
840
910, 40
1020, 40
1110, 20, 40
1230, 40
1310, 30, 40
1420, 30, 40
1510, 20, 30, 40
Sheet6
Cell Formulas
RangeFormula
C1:C15C1=DROP(MID(REDUCE(0,A1:A4,LAMBDA(a,c,VSTACK(a,a&", "&c))),4,9^9),1)
Dynamic array formulas.
Thanks.

Can't say I'm familiar withLAMBDA functions.

Would you have a VBA solution instead?

Just found this:

Code:
https://stackoverflow.com/questions/7198154/combination-algorithm-in-excel-vba
 
Upvote 0
Not sure if you wanted it a single string separated by commas or their cells. This is the comma-separated version.
VBA Code:
Sub GeneratePowerSet()
    Dim ws As Worksheet
    Dim lastRow As Long
    Dim dataRange As Range
    Dim dataArr() As Variant
    Dim powerSetStrings() As String
    Dim i As Long, j As Long, k As Long
    Dim numRows As Long
    Dim outputRange As Range
    Dim outputCell As Range

    Set ws = ThisWorkbook.Sheets("Sheet1") ' Replace "Sheet1" with your sheet name
    lastRow = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row
    Set dataRange = ws.Range("A2:A" & lastRow) 'Assumes data starts in A2
    dataArr = dataRange.Value
    numRows = UBound(dataArr, 1) - LBound(dataArr, 1) + 1
    ReDim powerSetStrings(1 To 2 ^ numRows - 1)

    For i = 1 To 2 ^ numRows - 1
        Dim subset As String
        subset = ""
        For j = 0 To numRows - 1
            If ((i And (2 ^ j)) > 0) Then
                If subset <> "" Then subset = subset & ", "
                subset = subset & dataArr(j + 1, 1)
            End If
        Next j
        powerSetStrings(i) = subset
    Next i
    
    'Output to C2
    Set outputRange = ws.Range("C2").Resize(UBound(powerSetStrings), 1)
    outputRange.Value = Application.Transpose(powerSetStrings)
    Set ws = Nothing
    Set dataRange = Nothing
    Erase dataArr
    Erase powerSetStrings
End Sub
 
Upvote 0
Solution
Not sure if you wanted it a single string separated by commas or their cells. This is the comma-separated version.
VBA Code:
Sub GeneratePowerSet()
    Dim ws As Worksheet
    Dim lastRow As Long
    Dim dataRange As Range
    Dim dataArr() As Variant
    Dim powerSetStrings() As String
    Dim i As Long, j As Long, k As Long
    Dim numRows As Long
    Dim outputRange As Range
    Dim outputCell As Range

    Set ws = ThisWorkbook.Sheets("Sheet1") ' Replace "Sheet1" with your sheet name
    lastRow = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row
    Set dataRange = ws.Range("A2:A" & lastRow) 'Assumes data starts in A2
    dataArr = dataRange.Value
    numRows = UBound(dataArr, 1) - LBound(dataArr, 1) + 1
    ReDim powerSetStrings(1 To 2 ^ numRows - 1)

    For i = 1 To 2 ^ numRows - 1
        Dim subset As String
        subset = ""
        For j = 0 To numRows - 1
            If ((i And (2 ^ j)) > 0) Then
                If subset <> "" Then subset = subset & ", "
                subset = subset & dataArr(j + 1, 1)
            End If
        Next j
        powerSetStrings(i) = subset
    Next i
   
    'Output to C2
    Set outputRange = ws.Range("C2").Resize(UBound(powerSetStrings), 1)
    outputRange.Value = Application.Transpose(powerSetStrings)
    Set ws = Nothing
    Set dataRange = Nothing
    Erase dataArr
    Erase powerSetStrings
End Sub
Thanks, this'll do nicely.
 
Upvote 0

Forum statistics

Threads
1,222,091
Messages
6,163,855
Members
451,861
Latest member
Lurch65

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