Sub TrialOne()
Dim loop_ctr As Integer
For loop_ctr = 1 To 100
Sheets("Capacity&Costs").Activate
Range("Vans") = "=INT(RAND()*3+1)-1" 'Generate random number
Range("c26:c29") = 0 'Account for large van's lead time
Range("d29") = 0 'Account for small van's lead time
Range("VanCapacity").Formula = "=Sum(g4:h4)" 'Calculate total van capacity
Range("LostBusiness").Formula = "=k4-l4" 'Calculate lost business
Range("VanPurchase").Formula = "=Sum(o4:p4)" 'Calculate Van Purchase
Range("RunningCost").Formula = "=Sum(S4:t4)" 'Calculate Running Cost
Range("LostBusinessCost").Formula = "=M4*B44" 'Calculate Lost Business Cost
Range("Cost").Formula = "=sum(q30,u30,w30)" 'Calculate Cost
Calculate
Next loop_ctr
End Sub
嗨,我有上面的代码。我的最终输出是"成本"如..所示。我想重复这个操作100次,所以我得到了100个不同的值" Cost"。现在这段代码重复了100次动作,但我最后得到的是" Cost的第100个值。"我想获得一个包含" Cost"的100个不同值的列表,这个列表如下所示:
Trial 1 - $1000
Trial 2 - $2000
Trial 3 - $3000
范围"货车与#34;在第四行是由0,1或2的整数组成。如果还有其他信息我应该提供,请告诉我。
答案 0 :(得分:2)
一个简单的方法就是在你的循环中,在计算成本后,将该值复制到一个新的单元格,在每次迭代中将其向下移动一个。
e.g。
Range("Cost").Formula = "=sum(q30,u30,w30)" 'Calculate Cost
Calculate
'write the cost to a cell
Range("A" & loop_ctr).Value = "Trial " & loop_ctr
Range("B" & loop_ctr).Value = Range("Cost").Value
Next loop_ctr