答案 0 :(得分:1)
这样做可以减少格式化
Public Sub PackingExample()
Dim Data As Variant, Pack As Variant
Dim i As Long, j As Long, PackTotal20 As Long, PackTotal22 As Long, PackTotal24 As Long, NextOrder As Long
With ActiveSheet
Data = Range(.Cells(2, 1), .Cells(.Cells(.Rows.Count, 1).End(xlUp).Row, 3)).Value2
ReDim Pack(1 To UBound(Data, 1))
For i = LBound(Data, 1) To UBound(Data, 1)
NextOrder = i
PackTotal20 = 0: PackTotal22 = 0: PackTotal24 = 0
Do
PackTotal20 = PackTotal20 + Data(NextOrder, 3)
PackTotal22 = PackTotal22 + Data(NextOrder, 3)
PackTotal24 = PackTotal24 + Data(NextOrder, 3)
NextOrder = NextOrder + 1
If NextOrder > UBound(Data, 1) Then Exit Do
Loop Until PackTotal24 + Data(NextOrder, 3) >= 24
i = NextOrder - 1
If PackTotal20 <= 20 Then
Pack(i) = 20
ElseIf PackTotal22 <= 22 Then
Pack(i) = 22
ElseIf PackTotal24 <= 24 Then
Pack(i) = 24
End If
Next i
With .Cells(1, 4)
Range(.Offset(1, 0), .Offset(UBound(Pack), 0)).Value2 = Application.Transpose(Pack)
End With
End With
End Sub