简体   繁体   English

Excel VBA-Vlookup和范围

[英]Excel VBA - Vlookup & range

I defined Cl as range and try to assign the range from the data in a table using VLookUp . 我将Cl定义为范围,并尝试使用VLookUp从表中的数据分配范围。

The result obtained from VLookUp is a range address (Example: e58 ). VLookUp获得的结果是范围地址(示例: 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. 这不是在VBA中运行公式的方式。 Some functions are available in the Application object (including VLookUp ). Application对象(包括VLookUp )中提供了某些功能。

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

For others use the Application.WorksheetFunction object 对于其他人,请使用Application.WorksheetFunction对象

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. Ant,是的,您应该在代码中添加错误处理。

Side note: even if your approach did work, you would need to delimit the " 's around a7:b28 , like this 旁注:即使您的方法行得通,您也需要像这样在a7:b28周围定界"

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

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

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