简体   繁体   中英

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

My code works when I use True on the last argument. 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

Unable to get vlookup property of the WorksheetFunction class

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. 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.

You could also do something like this using INDEX and MATCH ...

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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