简体   繁体   English

使用Excel VBA插入Vlookup公式

[英]Using Excel VBA to insert Vlookup formula

I'm having trouble using VBA to insert a vlookup formula in a column. 我在使用VBA在列中插入vlookup公式时遇到问题。 I get a "Run-time error '1004': Application-defined or object-defined error". 我得到一个“运行时错误'1004':应用程序定义或对象定义的错误”。 I suspect this could be an issue with the "double-quotes" thing. 我怀疑这可能是“双引号”的问题。 I've done some research on this, but still can't manage to wrap my head around when the double quotes are needed, and when they are not. 我已经对此做了一些研究,但是当需要双引号时,以及当它们不需要时,仍然无法绕过我的脑袋。 Also, I'm using a dynamic date variable (TodaysDate), a string determined by =Format(Date, "mm-dd-yyyy"). 另外,我使用的是动态日期变量(TodaysDate),一个由= Format(Date,“mm-dd-yyyy”)确定的字符串。 Below is the code line that's not working: 下面是不起作用的代码行:

Range("Y2:Y" & RowCount).Formula = "=IF(ISNA(VLOOKUP(X2,""'[PHO """ & TodaysDate & """.xlsx]Detail'!$C:$D""),1,FALSE)),""Yes"",""No"")"

Having worked on the same thing this past week I found it was easier to assign the file name (and in my case location) to a variable and place that into the vlookup instead of putting it all inside. 在过去的一周里我一直在做同样的事情,我发现将文件名(以及我的情况下的位置)分配给变量更容易,并将其放入vlookup而不是将其全部放入其中。 For example, I did the following and got past this hurdle. 例如,我做了以下事情并克服了这个障碍。

filename1 = "C:\Users\me\Documents\Custom Office Templates\DataValidation.xlsx" & "Sheet1'!$A:$C"

ActiveWorkbook.Worksheets("PipelineData").Range("I2:I" & lrow) = "=VLOOKUP(trim(A2),'" & filename1 & " ,2,FALSE)"

so yours might be 所以你的可能

 filename = "Pho" & TodaysDate & ".xlsx" & "Detail'!$C$D"
 Range("Y2:Y" & RowCount).Formula = "=IF(ISNA(VLOOKUP(X2,'" & filename & "),1,FALSE)),""Yes"",""No"")"

Dont' know if this counts as an answer but it might help you out. 不要知道这是否算作答案,但它可能会帮助你。

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

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