[英]Index value for - VBA Excel macro that changes value of cell and runs the model
我创建了一个宏,用于更改名为“option”的单元格中的值。 一旦值发生变化,模型就会发生变化以反映这一点。
例如:
a) Option 1: best case scenario sales -> Cell "option" input 1
b) Option 2: worst case scenario sales -> Cell "option" input 2
c) etc
然后宏将模型中的结果复制到新表中。 因此,例如,它将模型结果从名为“costs”的单元格(这是一个动态单元格,取决于模型吐出的内容)复制到一个名为“costs_1”的新单元格,该单元格将是静态的。 下面的代码示例。
宏工作得很好,但是如果我想输入 100 个选项,那么代码会很长。
有人可以帮助如何在代码中创建通用引用,例如 Dim i As Integer i = i + 1,它将运行到 100? 这会改变单元格名称,例如cost_i 然后它去,cost_1,cost_2 cost_3 ...等。
非常感谢您的帮助。
最好的问候简
Sub RunModel()
' RunModel Macro
....
'Choose Option 1
Range("option").Select
ActiveCell.FormulaR1C1 = "1"
'Copy costs when option 1 is selected to a new cell
[costs].Select
Selection.Copy
[costs_1].Select
Selection.PasteSpecial Paste:=xlPasteValues
'Copy number of customers when option 1 is selected to a new cell
[customers].Select
Selection.Copy
[customers_1].Select
Selection.PasteSpecial Paste:=xlPasteValues
....
etc
....
'Choose Option 2
Range("option").Select
ActiveCell.FormulaR1C1 = "2"
'Copy costs when option 2 is selected to a new cell
[costs].Select
Selection.Copy
[costs_2].Select
Selection.PasteSpecial Paste:=xlPasteValues
'Copy number of customers when option 2 is selected to a new cell
[customers].Select
Selection.Copy
[customers_2].Select
Selection.PasteSpecial Paste:=xlPasteValues
....
etc
....
Sub RunModel()
dim i as long
for i = 1 to 100 'Change this to whatever you need it to be
'No need to select and activate cells or use the clipboard, just copy the values
range("option").FormulaR1C1 = i
range("costs_" & i).value = range("costs").value
range("customers_" & i).value = range("customers").value
'Do some more stuff
next i
'Do some more stuff
end sub
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.