hi ahchin5 and welcome to the forum.
as you've no doubt discovered this is a rather complicated request. i looked around for a while but didn't find anything i considered perfect so i wrote some code which is also not perfect, but it's almost the weekend and it doesn't look like you're getting a better answer for now:
Sub create_stacks()
Dim rng As Range
Dim ws As Worksheet
Dim lr As Long
Dim stackrange As Variant
Set ws = Sheets("Sheet1")
lr = ws.Range("A" & Rows.Count).End(xlUp).Row
stackrange = Array(5, 7, 9, 11, 11, 9, 7, 5)
ws.Sort.SortFields.Clear
ws.Sort.SortFields.Add Key:=Range("B2:B" & lr) _
, SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:=xlSortNormal
With ws.Sort
.SetRange Range("A1:B26")
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
For Each rng In ws.Range("B2:B" & lr - 1)
If i = 8 Then i = 0
nextcol = stackrange(i)
nextrow = Cells(Rows.Count, nextcol).End(xlUp).Row + 1
Cells(nextrow, nextcol).Value = rng.Value
Cells(nextrow, nextcol).Offset(0, -1).Value = rng.Offset(0, -1).Value
i = i + 1
Next rng
End Sub
basically this just sorts the values largest to smallest and then drops them one at a time into each of your four buckets (excluding the smallest value at the end...) - the end result is not bad. i had some thoughts around identifying how far from the average each of the buckets is and trying to trade some values around to optimize them and / or looping through each individual value to see if moving it to another bucket would reduce the overall variance, but i didn't actually make any headway in the vba. Even with the additional effort to tweak the thing i don't think i can guarantee an optimal distribution though...
anyway, after all that, i thought perhaps this code would get you close enough to provide some value - from here you can fairly easily do a spot check and identify additional optimization opportunities so at least you're not just starting from scratch. Hope it helps.
Bookmarks