Generate Unique combinations in VBA of values with a sum constraint

DonKampfello

New Member
Joined
Apr 18, 2022
Messages
13
Office Version
  1. 365
Platform
  1. Windows
Hi Excel geniuses

I need help with generating unique combinations of values in 1 column:

A ("Width")
0.660
0.742
0.825
0.990
1.155
1.237
1.253

I have used the Code given by the following user. It generated the values in the orange table, which worked out okay:

However, I have encountered 3 problems:
1. The combinations are stored together in one single cell. I need them separated in different columns, so I can make a sum total of the combinations. The Blue table is my end goal.
2. The VBA code does not allow each input from the yellow column ("A") to be used more than once. I need each width to be used more than once.
3. I need a constraint inside the VBA code, so only combinations with sum total = < 3,850 are generated. Is this possible?

I made The Blue table manually to illustrate my end goal. I hope the task makes sense.

1664998082208.png


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 6
        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
 

Excel Facts

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.

Forum statistics

Threads
1,215,019
Messages
6,122,707
Members
449,093
Latest member
Mnur

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