Syntax - set variable to result of Vlookup

G

Guest

I need some syntax help please…trying to set a variable to the result of this
vlookup formula:

myVariable = "=IF(ISERROR(VLOOKUP($E2,mySheet!myNamedRange," &
myVariablePositon & ",FALSE)),0,VLOOKUP($E2,mySheet!myNamedRange," &
myVariablePositon & " ,FALSE))"

I’ve tried various forms of WorksheetFunction.Vlookup & can’t get it. I
could replace the variable myVariablePosition to a specific column number,
but it is important to me to return a zero in the event of an error.

Ultimately, I want to do something like:

For i = 2 to lstrow

myVariable1 = "=IF(ISERROR(VLOOKUP($E2,mySheet!myNamedRange," &
myVariablePositon1 & ",FALSE)),0,VLOOKUP($E2,mySheet!myNamedRange," &
myVariablePositon1 & " ,FALSE))"

myVariable2 = "=IF(ISERROR(VLOOKUP($E2,mySheet!myNamedRange," &
myVariablePositon2 & ",FALSE)),0,VLOOKUP($E2,mySheet!myNamedRange," &
myVariablePositon2 & " ,FALSE))"

myResult = myVariable1 + myVariable2

cells(i,6) = myResult

Next i

Thanks in advance!
 
G

Guest

Excellent. I had spent waaay to much time assuming that I had the
myVariable/vlookup part wrong!

One more question if I could - are myVariable1 and myVariable2 declared as a
strings then, or must they be a variant?

Thanks so much... I do appreciate it!
 
T

Tom Ogilvy

What ever is returned by the vlookup. If it will be a string, then use
string, but change the + to &

if they will return numbers, then dim it as double or long depending on
whether it will be decimal number or not.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top