[英]Runtime error 1004 when inserting formula into cell VBA Excel
I am getting runtime error 1004 when trying to insert a formula into a cell in VBA. 尝试将公式插入VBA中的单元格时,我收到运行时错误1004。
In my excel sheet I have a date in column A and a stock ticker in row 1 starting in column B every 3 columns, so B, E, H etc. 在我的Excel工作表中,我在A列中有一个日期,在第1行有一个股票代码,从B列开始,每3列,所以B,E,H等。
In cell C2 I am trying to divide the value in B2 by the value in row 2 under the column heading "FTSE". 在单元格C2中,我试图将B2中的值除以标题为“FTSE”的列下的第2行中的值。 Putting this formula into the cell directly works:
直接将此公式放入单元格中:
=IFERROR(B2/(VLOOKUP($A2,$A$2:$GMQ$261,MATCH("FTSE",$B$1:$GMQ$1,0)+1,FALSE)),"")
I am trying to do this using vba. 我试图用vba做到这一点。 This is the code I have:
这是我的代码:
Sub InsertFormula()
Range("C2").Select
ActiveCell.Formula = _
"=IFERROR(B2/(VLOOKUP($A2,$A$2:$GMQ$261,MATCH(""FTSE"",$B$1:$GMQ$1,0)+1,FALSE)),"")"
End Sub
You can reduce the confusion generated by double-double-quotes¹ with the TEXT function used as TEXT(,)
. 您可以使用
TEXT(,)
TEXT函数减少双引号¹产生的混淆。 This returns a zero-length string just as ""
does and there are no quotes to double up like """"
. 这将返回零长度字符串,就像
""
一样,并且没有引号可以加倍像""""
。
Range("C2").Formula = _
"=IFERROR(B2/(VLOOKUP($A2, $A$2:$GMQ$261, MATCH(""FTSE"", $B$1:$GMQ$1 ,0)+1, FALSE)), TEXT(,))"
'without the offset and subsequent adjustment with full column references
Range("C2").Formula = _
"=IFERROR(B2/VLOOKUP($A2, $A:$GMQ, MATCH(""FTSE"", $1:$1 ,0), FALSE), TEXT(,))"
¹ See How to create strings containing double quotes in Excel formulas? ¹ 请参见如何在Excel公式中创建包含双引号的字符串? for more examples.
更多例子。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.