[英]How can I use a variable in a formula vba excel run time error 1004
Im trying to call upon a variable which contains a cell address in a formula in VBA, but keep getting run time error 1004. Is the something wrong with my syntax? 我试图在VBA的公式中调用一个包含单元格地址的变量,但不断出现运行时错误1004。我的语法有问题吗?
ActiveCell.FormulaR1C1 = "=(RC[-1]/" & CellLocationCFYearTotal & ")* " & CellLocationCFYearDC & ""
Or have I not declared the variable correctly? 还是我没有正确声明变量?
Dim RowLocationCFYearIDC As Long 'can hold over 32000 if over this many rows
Dim ColumnLocationCFYearIDC As Integer 'columns won't exceed 256 in sheet
Dim CellLocationCFYearIDC As String
CellLocationCFYearIDC = ActiveCell.Address
RowLocationCFYearIDC = ActiveCell.Row
ColumnLocationCFYearIDC = ActiveCell.Column
Thanks! 谢谢!
When you are debugging look at the value of ActiveCell.Address
. 调试时,请查看ActiveCell.Address
的值。 It is a string. 它是一个字符串。 You are setting this to a variable declared as Dim ColumnLocationCFYearIDC As Integer
. 您将其设置为一个声明为Dim ColumnLocationCFYearIDC As Integer
的变量。 This will not work. 这是行不通的。 You need to declare your variable which will hold your address as either variant
or string
. 您需要声明变量,该变量会将您的地址保存为variant
或string
。
However, in your formula you use CellLocationCFYearDC
and CellLocationCFYearTotal
which are not declared (maybe you didn't paste everything?). 但是,在公式中使用未声明的CellLocationCFYearDC
和CellLocationCFYearTotal
(也许您没有粘贴所有内容?)。
Be aware that you cannot use R1C1 style cell references and A1 style references. 请注意,您不能使用R1C1样式单元格引用和A1样式引用。 You'll need to use .Address(ReferenceStyle:=xlR1C1)
when establishing your addressed. 建立地址时,需要使用.Address(ReferenceStyle:=xlR1C1)
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.