Custom VBA VLookup Formula

jOSEFFB

New Member
Joined
Aug 21, 2003
Messages
37
I'm having a hard time getting a custom vlookup formula to work.

I need to be able to change a part of the range in the vlookup dynamically based on a cell value.

This is what I came up with but it gives me a value error. Can anyone help?

Function VL(val As Variant, RNG As Range, col As Integer, BL As Boolean)
Dim Lookup As Range
Set Lookup = Range("'C:\AirlineData\Total O&D\[" & RNG & " Total O&D.xls]by market'!$B$1:$D$65536")
VL = WorksheetFunction.VLookup(val, Lookup, col, BL)
End Function
 

Excel Facts

VLOOKUP to Left?
Use =VLOOKUP(A2,CHOOSE({1,2},$Z$1:$Z$99,$Y$1:$Y$99),2,False) to lookup Y values to left of Z values.
This should work but it's not. I'm using excel 2007 and also my breakpoints dont seem to want to work in my ide

Function VL(val As Variant, RNG As String, col As Integer)
MsgBox RNG
Dim stringCombo As String
stringCombo = "'C:\AirlineData\Total O&D\[" & RNG & " Total O&D.xls]by market'!$B$1:$D$65536"
MsgBox stringCombo
VL = Application.WorksheetFunction.VLookup(val, stringCombo, col, 0)
End Function


All the msg boxs come up with the right data
 
Upvote 0
That won't work unless the other workbook is open and you remove the path and refer to a range object. You can't pass a string to the second argument of WorksheetFunction.Vlookup - it has to be a range object.
 
Upvote 0

Forum statistics

Threads
1,223,243
Messages
6,170,971
Members
452,371
Latest member
Frana

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top