简体   繁体   中英

Excel VBA - Vlookup & range

I defined Cl as range and try to assign the range from the data in a table using VLookUp .

The result obtained from VLookUp is a range address (Example: e58 ).

I am unable to assign it as a range.

Set Cl= Range ("=vlookup(i, Range("a7:b28"), 2, False)")

That's not how you run a formula in VBA. Some functions are available in the Application object (including VLookUp ).

Set cl = Range(Application.VLookup(i, Range("a7:b28"), 2, False))

For others use the Application.WorksheetFunction object

Set cl = Range(Application.WorksheetFunction.VLookup(i, Range("a7:b28"), 2, False))

Note: there are some subtle differences in the two approaches, notably on how the functions handle errors. Ant yes, you should add error handling to your code.

Side note: even if your approach did work, you would need to delimit the " 's around a7:b28 , like this

"=vlookup(i, Range(""a7:b28""), 2, False)"

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