VBA
Sub TrialOne()
Dim loop_ctr As Integer
For loop_ctr = 1 To 100
Sheets("Capacity&Costs").Activate
Range("LVanPurchase") = "=INT(RAND()*3+1)-1" 'Generate random number
Range("SVanPurchase") = "=INT(RAND()*3+1)-1" 'Generate random number
Range("d26:d29") = 0 'Account for large van's lead time
Range("e29") = 0 'Account for small van's lead time
Range("VanCapacity") = "=LVanCapacity + SVanCapacity" 'total Van Capacity
Range("LVanPurchaseCost") = "=LVanPurchase * LVanCost" 'Large Van Purchase
Range("SVanPurchaseCost") = "=SVanPurchase * SVanCost" 'Large Van Purchase
Range("LostBusiness") = "=DeliveryDemand - DeliveryCapacity" 'Lost Business
Range("TotalVanPurchase") = "=LVanPurchaseCost + SVanPurchaseCost"
Range("LVanRunningCost") = "=LVanCapacity * LVanRunningCostperVehicle" 'Large Van Running Cost
Range("SVanRunningCost") = "=SVanCapacity * SVanRunningCostperVehicle" 'Small Van Running Cost
Range("RunningCost").Formula = "=LVanRunningCost + SVanRunningCost" 'Running Cost
Range("Cost") = "=sum(R30,V30,X30)"
Calculate
Application.Calculation = xlCalculationManual
Sheets("Trials").Activate
Range("A" & loop_ctr).value = "Trial " & loop_ctr
Range("B" & loop_ctr).value = Range("Cost").value
Next loop_ctr
End Sub
Sub Optimise()
Sheets("Trials").Activate
Range("Minimum") = "=Min(CostTrials)"
End Sub
使用上面的代码,我最终得到一个数字"最小"和范围(" LVanPurchase")和范围(" SVanPurchase")是随机生成的数字为0,1或2。 "最小"是最终输出,Range(" LVanPurchase")和Range(" SVanPurchase")中生成的数字是输入变量。
现在我想提取为" LVanPurchase"生成的实际值(0,1或2)。和" SVanPurchase",特别是"最低"。这可能在VBA吗?我想过使用求解器并向后工作,但它似乎没有用。在VBA中还有另一种方法吗?
答案 0 :(得分:0)
时间进行一些优化,因为在您重新计算工作表之前,这些值是固定的。
首先设置容量和成本表,然后我们每次重新计算,然后检查我们是否有新的最小值。如果我们这样做,那么保存这些值。
接下来我们直接在试用表上设置值,因此我们来回走动时不会出现闪烁。
仅在最后我们切换到试用表才能看到结果。
这给出了一个如下所示的例程:
Sub TrialOne()
Dim loop_ctr As Integer
'stop auto calculation
Application.Calculation = xlCalculationManual
'set up the capacity and costs sheet - we only need to do it once,
' as the recalcuation will regenerate the random numbers
Sheets("Capacity&Costs").Activate
Range("LVanPurchase") = "=INT(RAND()*3+1)-1" 'Generate random number
Range("SVanPurchase") = "=INT(RAND()*3+1)-1" 'Generate random number
Range("d26:d29") = 0 'Account for large van's lead time
Range("e29") = 0 'Account for small van's lead time
Range("VanCapacity") = "=LVanCapacity + SVanCapacity" 'total Van Capacity
Range("LVanPurchaseCost") = "=LVanPurchase * LVanCost" 'Large Van Purchase
Range("SVanPurchaseCost") = "=SVanPurchase * SVanCost" 'Large Van Purchase
Range("LostBusiness") = "=DeliveryDemand - DeliveryCapacity" 'Lost Business
Range("TotalVanPurchase") = "=LVanPurchaseCost + SVanPurchaseCost"
Range("LVanRunningCost") = "=LVanCapacity * LVanRunningCostperVehicle" 'Large Van Running Cost
Range("SVanRunningCost") = "=SVanCapacity * SVanRunningCostperVehicle" 'Small Van Running Cost
Range("RunningCost").Formula = "=LVanRunningCost + SVanRunningCost" 'Running Cost
Range("Cost") = "=sum(R30,V30,X30)"
Range("Minimum") = "=Min(CostTrials)"
For loop_ctr = 1 To 100
Calculate ' generate new random numbers
Sheets("Trials").Range("A" & loop_ctr).Value = "Trial " & loop_ctr
Sheets("Trials").Range("B" & loop_ctr).Value = Range("Cost").Value
If Range("Minimum") = Sheets("Trials").Range("B" & loop_ctr).Value Then
'we have a new minimum: save the values
Sheets("Trials").Range("C2:C27") = Range("LVanPurchase")
Sheets("Trials").Range("D2:D27") = Range("SVanPurchase")
End If
Next loop_ctr
'go to our results sheet
Sheets("Trials").Activate
'and turn calculation back on
Application.Calculation=xlCalculationAutomatic
End Sub
它现在也在一个例程中,而不是两个单独的例程