2016-07-06 92 views
0

我试图调用一个包含VBA中的公式中的单元格地址的变量,但一直运行时错误1004。句法?如何在公式中使用变量vba excel运行时错误1004

ActiveCell.FormulaR1C1 = "=(RC[-1]/" & CellLocationCFYearTotal & ")* " & CellLocationCFYearDC & "" 

或者我没有正确声明变量?

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 

谢谢!

+1

您正在混合公式中的R1C1和A1样式。所以更改'ActiveCell.Address'到'ActiveCell.Address(1,1,xlR1C1)'见:https://msdn.microsoft.com/en-us/library/office/ff837625.aspx –

+0

非常感谢斯科特,只是回到这个和你的建议完美,因为我确定你知道。再次感谢! –

回答

0

当您正在调试时查看ActiveCell.Address的值。它是一个字符串。您将此设置为声明为Dim ColumnLocationCFYearIDC As Integer的变量。这不起作用。您需要申报将您的地址保存为variantstring的变量。

但是,在您的公式中,您使用的是CellLocationCFYearDCCellLocationCFYearTotal哪些没有声明(也许您没有粘贴所有内容?)。

请注意,您不能使用R1C1样式单元格引用和A1样式引用。建立您的地址时,您需要使用.Address(ReferenceStyle:=xlR1C1)

+0

感谢Brad给出了详细的答案。我刚回到这个项目,你的建议非常出色。非常感激! –