简体   繁体   English

在单元格中插入VLOOKUP公式

[英]Inserting VLOOKUP formula in a cell

I am trying to insert a VLOOKUP formula in a cell. 我正在尝试在单元格中插入VLOOKUP公式。 I am not even trying to substitute parameters yet. 我什至没有尝试替代参数。 I use this code: 我使用以下代码:

myCell.Offset(0, 4).Value = "=VLOOKUP(A2;AnotherWorksheet!$A$1:$B$231;2;FALSE)"

And I am receiving an error: 1004 我收到一个错误:1004

If I paste that exact formula in a cell, then the formula is recognized and accepted... Any hint? 如果我将确切的公式粘贴到单元格中,则该公式将被识别并接受...有任何提示吗? thanks! 谢谢!

First, use , and not ; 首先,使用,而不是; when separating the arguments. 分离参数时。 Second, you're looking for the .Formula property, not .Value . 其次,您要查找.Formula属性,而不是.Value .FormulaR1C1 may work, but for exactness, just use A1 -style formula writing for your VLOOKUP . .FormulaR1C1可能有效,但为精确起见,只需为VLOOKUP使用A1样式的公式编写即可。 See below. 见下文。

Code: 码:

Sub Voom()
    Range("A1").FormulaR1C1 = "=VLOOKUP(A2,""AnotherWorksheet!$A$1:$B$231"",2,FALSE)"
End Sub

Result: 结果:

在此处输入图片说明

It shows #N/A because, of course, I have no data available. 它显示#N/A因为,当然我没有可用的数据。 :) :)

For your exact code, just use: 对于您的确切代码,只需使用:

myCell.Offset(0, 4).Formula = "=VLOOKUP(A2,""AnotherWorksheet!$A$1:$B$231"",2,FALSE)"

Hope this helps. 希望这可以帮助。

You need to use the Formula property. 您需要使用Formula属性。

For example: 例如:

myCell.Offset(0, 4).Formula = "=VLOOKUP(A2,""AnotherWorksheet!$A$1:$B$231"",2,FALSE)"

From MSDN, the Formula property and the FormulaR1C1 property documentation. 从MSDN,可获取Formula属性FormulaR1C1属性文档。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM