简体   繁体   中英

Error 2042 ("N/A") in Excel VLookup() Worksheet Function

Need some help to figure out strange VBA VLOOKUP Type Missmatch error. The code is really simple, since sss0 is a random number and all I want is to find closest value in a range (sheet 'BMD_CDF', Range("A2:B999")). In the spreadsheet, I set format for Sheets("BMD_CDF").Range("A2:B999") to scientific already...

Dim LookUp_Range As Range
Dim sss0 As Double
Set LookUp_Range = Sheets("BMD_CDF").Range("A2:B999")

sss0=Application.WorksheetFunction.Max(Rnd(), 0.005)
Debug.Print Application.VLookup(sss0, LookUp_Range, 2, 0)

ERROR MSG

在此处输入图片说明

What Range looks like

在此处输入图片说明

The Error 2042 ("N/A") seems to be caused by the fact that the value returned by the Excel Worksheet function:

Aplication.WorksheetFunction.Max(Rnd(), 0.005)

which is always less than 1 will never get into specified range of values (>6) in column A . For testing purpose, try to substitute it with any number in that range of values in Column A , for example, sss0 =6.15 and modify the VLOOKUP() statement as following:

Debug.Print Application.VLookup(sss0, LookUp_Range, 2, 1)

(where 1 stands for logical TRUE ) to get it working, ie finding the closest value (not exact match) as per your definition.

Hope this may help.

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