Option Explicit
Sub collection()
Dim lr&, lc&, i&, j&, k&, sG&, sC&, max&, sum&
Dim rng, rng2, arr(), ch As String
Worksheets("Sheet1").Activate
lr = Cells(Rows.Count, "A").End(xlUp).Row
lc = Cells(2, Columns.Count).End(xlToLeft).Column
rng = Range("B2", Cells(lr, lc)).Value
rng2 = Range("B2", Cells(lr, lc)).Value
Worksheets("Sheet2").Range("A2:M100").ClearContents
ReDim arr(1 To lr - 1, 1 To lc - 1)
For i = 2 To lr - 1
max = 0
For j = 2 To lc - 3
For k = j + 1 To lc - 2
If rng(i, j) < rng(i, k) Then
max = rng(i, k): ch = rng(1, k)
rng(i, k) = rng(i, j): rng(1, k) = rng(1, j)
rng(i, j) = max: rng(1, j) = ch
End If
Next
Next
Next
With WorksheetFunction
For i = 2 To lr - 1
sG = .Min(rng(i, 1), rng(i, lc - 1)): rng(i, 1) = sG
sC = rng(i, lc - 1) - sG
sum = sG
For j = 2 To lc - 2
sG = .Min(Round(rng(i, j) * (1 - Range("M1")), 0), sC): rng(i, j) = sG
sC = sC - sG
sum = sum + sG
Next
rng(i, lc - 1) = sum
For j = 2 To lc - 2
For k = 2 To lc - 2
If rng2(1, j) = rng(1, k) Then
max = rng(i, j): ch = rng(1, j)
rng(i, j) = rng(i, k): rng(1, j) = rng(1, k)
rng(i, k) = max: rng(1, k) = ch
End If
Next
Next
Next
End With
Worksheets("Sheet2").Activate
Range("B2").Resize(i - 1, j).Value = rng
Range("A2").Resize(i - 1, 1).Value = Worksheets("Sheet1").Range("A2:A" & lr).Value
End Sub