VLookup in VBA

M

Mike H.

I am trying to use the Vlookup function in VBA and the lookup is to an
external workbook. I get a runtime Error 9 when setting the lookup range:

Set lookuprng = Workbooks("PRProc").Sheets("Accounts").Range("$a$1:$f$65000")

Actually, my reference is a named range so I'd rather refer to the name
instead, but not sure the syntax for that either:
Set lookuprng = Workbooks("PRProc").Sheets("Accounts").Range("PRLURng")
 
B

Bob Phillips

Set lookuprng = Workbooks("PRProc").Sheets("Accounts").Range("A1:F65000")

Why not just use whole columns

Set lookuprng = Workbooks("PRProc").Sheets("Accounts").Range("A:F")



--
HTH

Bob

(there's no email, no snail mail, but somewhere should be gmail in my addy)
 
M

Mike H.

I don't have a problem using the range, but I still get the run-time error 9,
subscript out of range when I use the line below:
Set lookuprng = Workbooks("PRProc").Sheets("Accounts").Range("A:F")

I am NOT in that workbook when I execute the code, if that makes a difference?
 
S

Stefi

Set lookuprng = Workbooks("PRProc.xls").Names("PRLUrng").RefersToRange

Regards,
Stefi

„Mike H.†ezt írta:
 
D

Dave Peterson

I don't think it was going through the names collection that solved the
problem. I think it was that Stefi added the .xls in the filename:

Set lookuprng = Workbooks("PRProc.xls").Sheets("Accounts").Range("a:f")
or
Set lookuprng = Workbooks("PRProc.xls").Sheets("Accounts").Range("prlurng)
 
S

Stefi

Partly yes, but there is another difference:

Workbooks("PRProc.xls").Names("PRLUrng").RefersToRange
doesn't require specifying sheet,

Workbooks("PRProc.xls").Sheets("Accounts").Range("prlurng")
does.

Regards,
Stefi

„Dave Peterson†ezt írta:
 
D

Dave Peterson

But I don't think that that is what made your code work. I bet it would have
had the same problem if you had used:

Workbooks("PRProc").Names("PRLUrng").RefersToRange
 
S

Stefi

Agreed! In fact I learned an optional referring method from you. Earlier I
always used
Workbooks("PRProc.xls").Names("PRLUrng").RefersToRange
style references, from now on I can consider using
Workbooks("PRProc.xls").Sheets("Accounts").Range("prlurng")
method.

Regards,
Stefi

„Dave Peterson†ezt írta:
 

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