0

我是 VBA/宏编码的新手。试图在 RC 参考中插入一个变量,但它不起作用。我不确定我在哪里犯了错误,感谢任何指导。

Dim var1 As Integer
Dim var2 As Integer
var1 = 1  'this changes dynamically in my actual program
var2 = 2  'this changes dynamically in my actual program
Range("A2").Select
'following doesn't work
ActiveCell.FormulaR1C1 = "=RC[var1]/RC[var2]"
'following works
ActiveCell.FormulaR1C1 = "=RC[1]/RC[2]"
4

2 回答 2

2

Variables don't expand within string literals. You should explicitly build the string:

ActiveCell.FormulaR1C1 = "=RC[" & CStr(var1) & "]/RC[" &CStr(var2) & "]"
于 2013-10-14T14:51:40.417 回答
0

Using string concatenation (&):

ActiveCell.FormulaR1C1 = "=RC" & var1 & "/RC" & var2
于 2013-10-14T14:51:00.483 回答