Hi everyone!
Several years ago, a user posted a question (773432) which was resolved: how to organize a list of values into two groups of (as close to) equal sum value. The answer was deftly provided by user "nimrod," who provided the following VBA code (see below).
A new user then asked the question which is also now my problem: What needs to be done to expand this VBA code to support X groups, rather than just two?
That question was not answered, but I would tremendously appreciate anyone's assistance in doing that now. For me, x=6, specifically, but it is beyond me to understand what would be needed to expand the two groups to six.
Thank you in advance! Code previously provided follows...
Scott
Public Sub DivideUpNumbers()
Dim HalfTotal As Long, iRow As Long, TmpTot As Long
' <<< CONFIG COLUMN HERE >>>
Const SrcNumCol As String = "A"
'Calc Mid point
HalfTotal = Application.WorksheetFunction.Sum(Range(SrcNumCol & ":" & SrcNumCol)) / 2
' sort data
With ActiveWorkbook.ActiveSheet.Sort
.SetRange Range(SrcNumCol & ":" & SrcNumCol)
.Header = xlNo
.MatchCase = False
.Orientation = xlTopToBottom
.Apply
End With
' Assign Numbers to one of two subgroups
For iRow = 1 To Cells(Rows.Count, SrcNumCol).End(xlUp).Row
If IsNumeric(Cells(iRow, SrcNumCol).Value) Then
If Cells(iRow, SrcNumCol).Value + TmpTot <= HalfTotal Then
TmpTot = TmpTot + Cells(iRow, SrcNumCol).Value
Cells(iRow, SrcNumCol).Offset(0, 1).Value = "Grp-A"
Else
Cells(iRow, SrcNumCol).Offset(0, 1).Value = "Grp-B"
End If
End If
Next iRow
End Sub
Bookmarks