Vlookup宏错误

时间:2018-04-02 21:43:47

标签: excel vba excel-vba vlookup

以下是我 vlookup 代码,我在*内的文字上一直收到错误消息。

我正在尝试使用vlookup来查看使用其他按钮选择的随机词并提取定义。

在Excel中添加常规vlookup代码: =VLOOKUP(B2,Sheet1!A2:B500,2,FALSE)

Sub Definition2()

Dim lookupvalue As Variant
Dim lookuprange As Variant

lookupvalue = Sheet2.Range("B2").Value
Set lookuprange = Sheet1.Range("A2:B500")

'- 2: execute vlookup functionwith variables above -'
vresult = Application.VLookup(lookupvalue, lookuprange, 2, False)

Range("J3").Value = vresult

End Sub

1 个答案:

答案 0 :(得分:1)

更新的答案

不是使用行指定范围,而是将vlookuprange设置为整列

第一个答案

您不能“设置”声明为字符串的变量,因为它不是对象

Sub Definition2()

Dim lookupvalue As String
Dim lookuprange As Range
Dim lookupcolnum As Single

lookupvalue = Sheet2.Range("B3").Value
Set lookuprange = Sheet1.Range("A:B")

'- 2: execute vlookup functionwith variables above -'
vresult = Application.VLookup(lookupvalue, lookuprange, 2, False)

Range("J3").Value = vresult

End Sub
相关问题