Assign Parties to Groups with Maximum Size

Barklie

Board Regular
Joined
Jul 4, 2013
Messages
86
Greetings,

I have been attempting unsuccessfully for a method to assign parties of random sizes into groups of a maximum size with the goal of minimizing the total number of groups. I am open to using formulas or VBA for solving this problem.

Data Example.xlsx
ABCDEFGHI
1Party IDSizeGroup IDSizeParties
2A1Max Group Size13Group A13A, H
3B10Min Group #8Group B13B, L
4C4Group C12C, J
5D4Group D12D, I
6E13Group E13E
7F11Group F11F
8G10Group G10G
9H12Group H10K
10I4
11J8
12K10
13L3
Sheet1


Truthfully, I have yet to think of a fail-proof way to this conceptually. Thanks for any help!

Regards,
Barklie Estes
 

Excel Facts

Back into an answer in Excel
Use Data, What-If Analysis, Goal Seek to find the correct input cell value to reach a desired result
Please try this:

It assumes the group list with party size begins at cell A2. Output to G2. Sorting the list maximizes the chances of getting group sizes near the max


VBA Code:
Sub GroupByMax()
  Dim Cel As Range
  Dim Rng As Range
  Dim OutRng As Range         'Top left cell of new list
  Dim vAry As Variant
  Dim X As Long
  Dim Grp As String
  Dim PrtySize As Long        'Current Party size
  Dim Acct() As Boolean       'Array to set individual status of groups
  Dim GrpCnt As Long
  Dim MaxGrpSize As Long
  Dim OutCnt As Long
  Dim Grps As String          'Many groups
  Dim GrpSize As Long         'Current Group party size
  Dim Y As Long
  Dim AcctCnt As Long         'Store count of group status
  Dim Sht As Worksheet
  Dim Key As Range
  
  MaxGrpSize = 13             'Add your code to get the value from the worksheet
  
  Set Sht = ActiveSheet
  
  Set Rng = Range("A2:B27")               'Full set of groups and party size
  Set Key = Range("B2:B27")               'Party Size column
  
  Sht.Sort.SortFields.Clear               'Sort by party size largest on top
  Sht.Sort.SortFields.Add2 Key:=Key, SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:=xlSortNormal
  With Sht.Sort
    .SetRange Rng
    .Header = xlGuess
    .MatchCase = False
    .Orientation = xlTopToBottom
    .SortMethod = xlPinYin
    .Apply
  End With
  
  
  Set OutRng = Range("G1")                                '<--- Change this
  
  Application.EnableEvents = False
  Application.Calculation = xlCalculationManual
  
  vAry = Application.Transpose(Application.Transpose(Rng))
  GrpCnt = UBound(vAry)
  ReDim Acct(1 To GrpCnt)
  For X = 1 To GrpCnt                                       'Go through each Group
    Grp = vAry(X, 1)
    PrtySize = vAry(X, 2)
    Grps = Grp
    GrpSize = PrtySize
    
    If Acct(X) = False Then
      
      If PrtySize >= MaxGrpSize Then                      'Already max party size
        OutCnt = OutCnt + 1
        OutRng.Offset(OutCnt, 0).Value = Grps             'Add to new list
        OutRng.Offset(OutCnt, 1).Value = PrtySize
        Acct(X) = True
        AcctCnt = AcctCnt + 1
      ElseIf PrtySize < MaxGrpSize Then                    'Go get more groups to get add
        For Y = X + 1 To GrpCnt
          If Acct(Y) = False And vAry(Y, 2) <= (MaxGrpSize - GrpSize) Then    'Will it fit?
            Grps = Grps & ", " & vAry(Y, 1)
            GrpSize = GrpSize + vAry(Y, 2)
            Acct(Y) = True
            AcctCnt = AcctCnt + 1
            If GrpSize = MaxGrpSize Then Exit For       'Maxed out
          End If
        Next Y
        OutCnt = OutCnt + 1                             'add to new list
        OutRng.Offset(OutCnt, 0).Value = Grps
        OutRng.Offset(OutCnt, 1).Value = GrpSize
      End If
    End If
    
    If AcctCnt = GrpCnt Then Exit For
    
    
  Next X
  
  Application.EnableEvents = True
  Application.Calculation = xlCalculationAutomatic
  
End Sub
 
Upvote 0
Solution
Jeffrey,

That's very clever. If I understand correctly, you sort largest to smallest, try to add the largest, non-max group to the second largest, and move down from there. There are probably some weird instances where this wouldn't optimize (like parties of 7,5,4,4,3,3), but this is more than sufficient for my application where I will be using it to parallel electrical circuits into groups 20amps or under.

Thank you greatly.

Regards,
Barklie Estes
 
Upvote 0

Forum statistics

Threads
1,215,072
Messages
6,122,968
Members
449,095
Latest member
Mr Hughes

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