将公式插入单元格VBA Excel时的运行时错误1004 [英] Runtime error 1004 when inserting formula into cell VBA Excel
问题描述
在我的excel表中,我有一个日期在列A和一行的股票行情从列B开始每3列,所以B,E,H等。 / p>
在单元格C2中,我试图将B2中的值除以FTSE列的第2行中的值。将此公式放入单元格中直接起作用:
= IFERROR(B2 /(VLOOKUP($ A2,$ A $ 2:$ GMQ $ 261,MATCH(FTSE,$ B $ 1:$ GMQ $ 1,0)+ 1,FALSE)),)
我试图用vba这样做。这是我的代码:
Sub InsertFormula()
范围(C2)。选择
ActiveCell.Formula = _
= IFERROR(B2 /(VLOOKUP($ A2,$ A $ 2:$ GMQ $ 261,MATCH(FTSE,$ B $ 1:$ GMQ $ 1, 0)+ 1,FALSE)),)
End Sub
您可以通过 TEXT功能用作 TEXT(,)
的B46E545-E612-4C84-AC23-EDFA68007945。这将返回一个零长度的字符串,就像一样,没有引号像
。
Range(C2)。Formula = _
= IFERROR(B2 /(VLOOKUP A2,$ A $ 2:$ GMQ $ 261,MATCH(FTSE,$ B $ 1:$ GMQ $ 1,0)+1,FALSE)),TEXT(,))
后续的调整与完整的列引用
范围(C2)。Formula = _
= IFERROR(B2 / VLOOKUP($ A2,$ A:$ GMQ,MATCH(FTSE,$ 1 :$ 1,0),FALSE),TEXT(,))
¹请参阅 如何在Excel公式中创建包含双引号的字符串 有关更多示例。
I am getting runtime error 1004 when trying to insert a formula into a cell in VBA.
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.
In cell C2 I am trying to divide the value in B2 by the value in row 2 under the column heading "FTSE". 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. 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(,)
. 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? for more examples.
这篇关于将公式插入单元格VBA Excel时的运行时错误1004的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!