2013-10-14 76 views
0

我是VBA /宏編碼的新手。嘗試在RC引用中插入一個變量,但它不起作用。我不知道我在哪裏犯了一個錯誤,任何指導表示讚賞。在excel中使用變量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]" 

回答

2

變量不字符串文字內展開。您應該明確建立字符串:

ActiveCell.FormulaR1C1 = "=RC[" & CStr(var1) & "]/RC[" &CStr(var2) & "]" 
0

使用字符串連接(&):

ActiveCell.FormulaR1C1 = "=RC" & var1 & "/RC" & var2