简体   繁体   English

Vlookup完全匹配(false)的VBA代码应该有效吗?

[英]VBA code for Vlookup exact match (false) should work?

My code works when I use True on the last argument. 当我在最后一个参数上使用True时,我的代码有效。 The problem is that it must be an exact match, otherwise the code brings me the incorrect values. 问题是它必须是完全匹配,否则代码会给我带来不正确的值。 However, when I change the last argument to False I get the error 1004 但是,当我将最后一个参数更改为False时,我得到错误1004

Unable to get vlookup property of the WorksheetFunction class 无法获取WorksheetFunction类的vlookup属性

Here is my code: 这是我的代码:

Range("AW" & i) = WorksheetFunction.VLookup(Sheet2.Range("B" & i), Sheet3.Range(Sheet3.Range("A1"), Sheet3.Range("B" & lastrow)), 2, False)

I just want to make this vlookup give me the correct values. 我只是想让这个vlookup给我正确的值。 Therefore, I need to make the exact match argument work. 因此,我需要使完全匹配参数工作。

You need to do something like this ... 你需要做这样的事......

Range("AW" & i) = WorksheetFunction.VLookup(Sheet2.Range("B" & i), Sheet3.Range("A1:B" & lastrow), 2, False)

... you need to wrap the single range in the second parameter. ...你需要在第二个参数中包装单个范围。 It's not 100% tested but I mocked something up locally working on a single sheet that worked. 它没有经过100%的测试,但是我在当地工作的一块工作模拟了一些东西。

You could also do something like this using INDEX and MATCH ... 你也可以用INDEXMATCH这样做......

Range("AW" & i) = WorksheetFunction.Index(Sheet3.Range("A1:B" & lastrow), WorksheetFunction.Match(Sheet2.Range("B" & i), Sheet3.Range("A1:B" & lastrow)))

If you have the potential for items not finding a match, you'll need to perform the appropriate error checking. 如果您有可能找不到匹配的项目,则需要执行相应的错误检查。

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

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