VBA检查所有选项,选择最便宜的

时间:2018-10-26 10:39:23

标签: excel vba excel-vba

我的工作簿中有两个表。 第一个表格(A1:D10)显示了计划在特定日期制造的所有机器(AGA开始) 第二个表(F1:I10)显示在特定日期(POS结束)准备就绪的所有存储。

enter image description here

每天不使用存储空间会花费成本(在我的测试中,每天5美元)。现在,我想要一个宏,它会尝试所有可能的方式将存储分配到显示最便宜方式的机器上。

下面的代码仅显示将TSP1用于Machine1 /将TSP2用于machine2 / ... /将TSP10用于Machine10的成本:

Sub TSP_Simulation()
    Dim TSP_Lastrow As Long, Machine_Lastrow As Long
    Dim Day_min() As Long, tsp_nr() As Long, machine_nr() As Long _
, Costs() As Long
    Dim tsp() As String, machine() As String
    Dim size_tsp As Integer, size_machine As Integer, TSP_Costs As Integer, Not_available As Integer
    Dim TSP_Range As Range, machine_Range As Range


    size_tsp = WorksheetFunction.CountA(Worksheets(1).Columns(6))
    size_machine = WorksheetFunction.CountA(Worksheets(1).Columns(1))

    TSP_Lastrow = ActiveSheet.Cells(Rows.Count, "I").End(xlUp).Row
    Machine_Lastrow = ActiveSheet.Cells(Rows.Count, "D").End(xlUp).Row

    ReDim tsp(size_tsp), machine(size_machine) _
    , tsp_nr(size_tsp), machine_nr(size_machine) _
    , Day_min(size_tsp), Costs(size_machine)

    b = 2
    For a = 2 To TSP_Lastrow
        c = 0
    ' Choose first TSP:
        Set TSP_Range = Cells(a, 8)
        tsp(a) = Cells(a, 6).Value
        tsp_nr(a) = Cells(a, 7).Value

' Check if Machine has no TSP:
        For b = 2 To Machine_Lastrow
            If IsEmpty(Cells(b, 3)) Then
                DayDiff = DateDiff("D", Cells(a, 9).Value, Cells(b, 4).Value)
                If DayDiff < 0 Then
                    c = c + 1
                    GoTo Line1
                End If
                TSP_Costs = DayDiff * 5
                Costs(c) = TSP_Costs
                machine(c) = Cells(b, 1)
                machine_nr(c) = Cells(b, 2)
            Else: GoTo Line1
            End If
            c = c + 1
Line1:  Next b

        y = Application.Min(Costs)
        Range("J" & a) = Costs(y)
        Range("C" & a) = tsp_nr(a)
        TSP_Range.Value = machine_nr(y)
    Next a
End Sub

希望有人可以帮助我。

0 个答案:

没有答案