Using vlookup in VB

K

kwiklearner

I am trying to create a range where it can be used in a vlookup formul
through VBA.
What I have so far:

Dim irange as range
Set irange = activesheet.range("A1:B10") 'range is in mybook.xls
sheet1

the following formula will be populated in a separate workbook vi
VBA:
I've tried:
range("D4").formula="=vlookup(rc3" & irange & "2,false)"
range("D4").formula="=vlookup(rc3,'[mybook.xls]sheet1'!" & irange
",false)"
range("D4").formula="=vlookup(rc3,'[mybook.xls]" & irange & "2,false)"

For all the formulas above, I get a run-time 13 error, type mismatch.


Any help will be very much appreciated...
Thanks in advance...
 
G

Guest

The formula property is looking for a string. You should dim your irange as
a string and just make it equal to "A1:B10".
So basically you should have:
"=vlookup(rc3," & irange & ",2,false"
Also, I'm not sure what "rc3" is. Is that a named ranged in your sheet? If
that's not a valid range name, you'll need to edit that too.

-Simon
 
D

Dave Peterson

Since you're using .formula, you don't want to use R1C1 reference style.

You could either change to .formular1c1 (and fix the formula) or change the
formula to use A1 reference style (and fix the formula).

Range("D4").Formula = "=vlookup(rc3" & iRange & "2,false)"
could become
Range("D4").Formula _
= "=vlookup(C4," & iRange.Address(external:=True) & ",2,false)"
or
Range("d4").FormulaR1C1 _
= "=vlookup(rc3," & iRange.Address(ReferenceStyle:=xlR1C1, external:=True) _
& ",2,false)"


As aside, it's probably better to copy directly from your code and paste into
your message. It may prevent a typo and save you time getting a response--not
saying that your code had typos this time.

I am trying to create a range where it can be used in a vlookup formula
through VBA.
What I have so far:

Dim irange as range
Set irange = activesheet.range("A1:B10") 'range is in mybook.xls,
sheet1

the following formula will be populated in a separate workbook via
VBA:
I've tried:
range("D4").formula="=vlookup(rc3" & irange & "2,false)"
range("D4").formula="=vlookup(rc3,'[mybook.xls]sheet1'!" & irange &
",false)"
range("D4").formula="=vlookup(rc3,'[mybook.xls]" & irange & "2,false)"

For all the formulas above, I get a run-time 13 error, type mismatch.

Any help will be very much appreciated...
Thanks in advance....
 

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