如何创建一个excel公式/表格,根据长度计算出我需要多少东西?

时间:2017-01-31 17:57:02

标签: excel excel-formula

我是一家定制金属加工公司的项目经理。 当我计算出我们需要多少结构管道,通道,角度等时,我就是这样做的:

  • 写出每个部分的数量和长度。例如:
    • 1x 110“
    • 4x 107“
    • 3x 50“
    • 18x 9“
  • 从288开始(通常)并减去长度,从最长开始。所以我从288开始,然后减去110直到我不能再这样做,然后减去较小的值,直到我完全不能再适合通过上面的例子,它将变成288> 178> 71> 21> 12> 3.这将是第一个棒。我一直这样做,直到我没有零件,这告诉我需要订购多少支。

我希望能够在excel中做到这一点。我试图解决这个问题,但无济于事。

所以基本上我需要什么样的公式才能做到这一点?基本上我可以输入零件的长度和数量以及棒的长度,并计算所需的棒数。

注意: 仅通过将所有长度加在一起并除以棒长度,它绝对不会起作用。它必须递归(是正确的单词?)从棒长度中减去零件长度。

1 个答案:

答案 0 :(得分:2)

我考虑过你的问题并且我喜欢这个任务,所以我决定写一个快速的宏来帮助解决你的问题。您对以下宏的输入应如下所示:

enter image description here

宏的代码如下所示:

Sub CalcPipe()

Dim StartPipeLng As Long, LastLng As Long, TotalLng As Long, PipeName As Long

StartPipeLng = Cells(2, 4).Value ' Picks start value from cell D2

LastLng = ActiveSheet.UsedRange.Rows(ActiveSheet.UsedRange.Rows.Count).Row 'Find last row of loop

TotalLng = Application.WorksheetFunction.SumProduct(Range("A2:A" & LastLng), Range("B2:B" & LastLng))
TotalLng = Application.WorksheetFunction.RoundUp(TotalLng / StartPipeLng, 0)

Cells(5, 4).Value = TotalLng 'Outputs the number of pipes in cell D5

PipeName = 1

    For y = 6 To TotalLng + 5

        Cells(1, y).Value = "Pipe " & PipeName
        PipeName = PipeName + 1
        StartPipeLng = Cells(2, 4).Value

        For x = 2 To LastLng
            If StartPipeLng - Cells(x, 1).Value >= 0 And Cells(x, 2).Value <> Cells(x, 3).Value Then
                StartPipeLng = StartPipeLng - Cells(x, 1).Value
                Cells(x, 3).Value = Cells(x, 3).Value + 1
                Cells(x, y).Value = Cells(x, y).Value + 1
                x = x - 1
            End If
        Next x
    Next y

Columns(3).Clear

End Sub

运行宏后的输出如下所示:

enter image description here

让我知道宏是否适合您。请注意,Stack Overflow不是一种编码服务,我是一次性为您写的。