基于值的限制在Microsoft Excel中自动创建行

3
我希望根据每个特定商品的最大单位数量创建纸盒。
例如:
Item    Quantity    MaxQtyPerCarton
A          12            5
B           6            3

这应该通过在Excel中创建行来产生以下结果。
Item    CartonQuantity
A          5
A          5
A          2
B          3
B          3

您可以看到,基于每箱最大数量(MaxQtyPerCarton),商品A的数量被分成了三行。同样地,商品B也被分成了两行。
有什么想法吗?

抱歉,在我的搜索中我没有看到这个问题被问过,所以我在这里发布了。这个问题还有效吗? - emnzava
4个回答

1

使用VBA方法(仅编程)

Option Explicit

Sub Sub1()
  Dim iRow1&, iRow2&, zItem$, zQuan&, zMaxQ&, zAmt&
  iRow2 = 10 ' ??
  For iRow1 = 2 To 3 ' ??
     zItem = Cells(iRow1, 1)
     zQuan = Cells(iRow1, 2)
     zMaxQ = Cells(iRow1, 3)
     Do While zQuan > 0
       zAmt = zQuan
       If zAmt > zMaxQ Then zAmt = zMaxQ
       Cells(iRow2, 1) = zItem
       Cells(iRow2, 2) = zAmt
       iRow2 = iRow2 + 1
       zQuan = zQuan - zAmt
     Loop
  Next iRow1
End Sub

0
假设您提供的表格位于A1:C3(第1行为标题),请在E2中输入此数组公式**
=IFERROR(INDEX($A$2:$A$3,MATCH(TRUE,MMULT(0+(ROW($A$2:$A$3)>=TRANSPOSE(ROW($A$2:$A$3))),CEILING($B$2:$B$3/$C$2:$C$3,1))>=ROWS($1:1),0)),"")

复制下来,直到你开始得到结果为空为止。

然后,在F2中使用这个(非数组)公式:

=IF(E2="","",MIN(INDEX($C$2:$C$3,MATCH(E2,$A$2:$A$3,0)),INDEX($B$2:$B$3,MATCH(E2,$A$2:$A$3,0))-INDEX($C$2:$C$3,MATCH(E2,$A$2:$A$3,0))*(COUNTIF($E$2:$E2,E2)-1)))

根据需要复制下来。

**数组公式的输入方式与“标准”公式不同。您需要按住CTRL和SHIFT键,然后再按ENTER键。如果操作正确,您会注意到Excel在公式周围放置了花括号 {}(但不要尝试手动插入这些内容)。


0

这不是非常优雅,但它可以完成工作。它要求您在问题中的第一个表结构(包括标题)在range("A1:C3")中。并且输出到列EF

Option Explicit

Sub FillCartons()
    Dim cll As Range
    Dim rng_Items As Range
    Dim rng_Quantity As Range
    Dim rng_MaxQty As Range
    Dim l_CartonCount As Long
    Dim l_AlreadyInCartons As Long

    Set rng_Items = Range(Range("A2"), Range("A1000000").End(xlUp))
    l_CartonCount = 1
    Range("E:F").ClearContents

    For Each cll In rng_Items
        Set rng_Quantity = cll.Offset(, 1)
        Set rng_MaxQty = cll.Offset(, 2)
        l_AlreadyInCartons = Application.WorksheetFunction.SumIf(Range("E:E"), cll.Value, Range("F:F"))

        Do Until l_AlreadyInCartons = rng_Quantity.Value
            If rng_Quantity.Value - l_AlreadyInCartons > rng_MaxQty.Value Then
                Cells(l_CartonCount, 5).Value = cll.Value
                Cells(l_CartonCount, 6).Value = rng_MaxQty.Value
            Else
                Cells(l_CartonCount, 5).Value = cll.Value
                Cells(l_CartonCount, 6).Value = rng_Quantity.Value - l_AlreadyInCartons
            End If
            l_CartonCount = l_CartonCount + 1
            l_AlreadyInCartons = Application.WorksheetFunction.SumIf(Range("E:E"), cll.Value, Range("F:F"))
        Loop
    Next cll
End Sub

0

假设您已经按照以下设置,并且您想要以 A10 开头的输出

enter image description here

我使用了取模和除法的组合,其中除法用于重复,取模用于余数。

Sub create_rows()

Dim arr()
arr = Range("A2:C5")
Range("A10").Select

Dim j
    For j = LBound(arr, 1) To UBound(arr, 1)               

          Dim looper, z
          looper = arr(j, 2) / arr(j, 3) 'No of times to print
          Modder = arr(j, 2) Mod arr(j, 3) 'Leftovers


           For z = 1 To looper
            ActiveCell = arr(j, 1) 'Name of the quantity
            ActiveCell.Offset(0, 1) = arr(j, 3) 'always the max per qtn number
            ActiveCell.Offset(1, 0).Select
           Next z

           If (Modder > 0) Then 'there is leftover quantity 
            ActiveCell = arr(j, 1)
            ActiveCell.Offset(0, 1).Value = arr(j, 2) - arr(j, 3) * Round(looper, 0)
            ActiveCell.Offset(1, 0).Select
           End If

        Next j        

End Sub

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接