Vba 将数量指定给标准批量

Vba 将数量指定给标准批量,vba,excel,automation,Vba,Excel,Automation,我有20、22和24个托盘的标准批量,我需要合并部分订单以自动满足批量 请使用vba解决方案提供帮助 前后插图粘贴在下面 这就是你想要的减去格式 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 Lo

我有20、22和24个托盘的标准批量,我需要合并部分订单以自动满足批量

请使用vba解决方案提供帮助

前后插图粘贴在下面


这就是你想要的减去格式

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

欢迎来到SO StackOverflow!嗯,这不是一个代码编写服务,这种没有尝试和尝试的问题通常不受欢迎,而且往往会吸引反对票。在发布问题之前,请进行研究,尝试一些东西,然后发布你的问题,并附上你迄今为止尝试过的总结和你面临的具体问题。另外,请检查我们的。