简体   繁体   English

Excel 2013 VBA Vlookup-功能错误

[英]Excel 2013 VBA Vlookup-Function Error

I wrote a program, which creates a resource-Overview. 我编写了一个程序,该程序创建了一个资源概述。 At a point in the program i want to set a vlookup-function via VBA. 在程序中,我想通过VBA设置vlookup-function。 If I set the vlookup-function manually in the sheet (write the formula into the cell)( =SVERWEIS(B2;'MS Project'!A:H;2;FALSCH) )("SVERWEIS" is the german word for "VLOOKUP", "FALSCH" is the german word for "FALSE") it works fine, but if I'm use the following line of code to insert this Formula automatically into the cell, it does not. 如果我在工作表中手动设置vlookup-function(将公式写入单元格中)( =SVERWEIS(B2;'MS Project'!A:H;2;FALSCH) )(“ SVERWEIS”是德语单词,表示“ VLOOKUP “,“ FALSCH”是德语单词“ FALSE”),它工作正常,但是如果我使用下面的代码行将此公式自动插入到单元格中,则不会。

rngTarget.Cells(i, col + 1).value = WorksheetFunction.VLookup(rngTarget.Range("B" & i), _
ThisWorkbook.Worksheets("MS Project").Range("A1:H2000"), 2, 0)

"rngTarget" represents the usedRange of the activeSheet. “ rngTarget”表示activeSheet的usedRange。

"i" represents the current row I'm working with. “ i”代表我正在使用的当前行。

The Image below shows the runtime-error (1004) I get while executing my program. 下图显示了我在执行程序时遇到的运行时错误(1004)。

在此处输入图片说明

It says, that the Vlookup-Property of the WorksheetFunction-Object could not be assigned. 它说,无法分配WorksheetFunction-Object的Vlookup-Property。

Edit: before this line gets executed, I check if the searched object exist in "Ms Project". 编辑:执行此行之前,我检查搜索的对象是否存在于“ Ms Project”中。 Only if it does exist, it will set the vlookup-function. 仅当它确实存在时,它才会设置vlookup-function。

SOLUTION: I figured out the problem, I changed: 解决方案:我发现了问题,我进行了更改:

rngTarget.Cells(i, col + 1).value = WorksheetFunction.VLookup(rngTarget.Range("B" & i), _
ThisWorkbook.Worksheets("MS Project").Range("A1:H2000"), 2, 0)

to: 至:

rngTarget.Cells(i, col + 1).value = Application.VLookup(rngTarget.Range("B" & i).value, _
ThisWorkbook.Worksheets("MS Project").Range("A1:H2000"), 2, 0)

For the Lookup_value argument you are referencing the whole object rngTarget.Range("B" & i) , try adding .Value2 to return only the value for this range. 对于Lookup_value参数,您引用的是整个对象rngTarget.Range("B" & i) ,请尝试添加.Value2以仅返回此范围的值。

ie

rngTarget.Cells(i, col + 1).value = WorksheetFunction.VLookup(rngTarget.Range("B" & i).Value2, _
ThisWorkbook.Worksheets("MS Project").Range("A1:H2000"), 2, 0)

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

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