AC130
AC130

Reputation: 35

Vlookup Macro Erroring Out

Below is my vlookup code for my macro and I keep getting an error on the text inside the *.

I am trying to use a vlookup that will see the random word selected with another button and pull the definition.

Adding in regular vlookup code in Excel: =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

Upvotes: 1

Views: 45

Answers (1)

warner_sc
warner_sc

Reputation: 848

UPDATED ANSWER

Instead of specifying the range with the rows set the vlookuprange as the whole column

FIRST ANSWER

You can't "set" a variable declared as a string since is not an object

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

Upvotes: 1

Related Questions