UDF using Vlookup returning a VALUE error

Pauline10

New Member
Joined
Feb 22, 2021
Messages
9
Office Version
  1. 365
Platform
  1. Windows
Hello everyone,

I'm a debutant in VBA (and I'm French, so please pardon the English errors coming ahead !) and am trying to create a UDF including a vlookup.
The problem is that it returns a VALUE error.
I frequently have to vlookup values in a recurrent table, and so to save time I would like to create a UDF that already includes the reference to this table, the only variable would be the lookup value :

Function Vachercher(a As Variant)

x = Workbooks("C:\Users\julie DUPRE\Documents\21-02-17 - FACTURES 14-21.xlsx").Worksheets("Restitution").Range("B2:BT36000")

Vachercher = Application.WorksheetFunction.VLookup(a, x, 58, False)

End Function

I don't understand the VALUE error...I've checked several times that my references and my range are correct and it seems there are. I've also tried the same UDF but without an external reference to a workbook to see if that was the problem (so i tried the udf within the worksheet containing the table) but with no luck.

I would greatly appreciate some help !

Pauline
 

Some videos you may like

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.

kweaver

Well-known Member
Joined
May 8, 2018
Messages
1,583
Office Version
  1. 365
  2. 2010
I have tried a number of variations of this without success. What am I doing incorrectly?

Code:
Function vlook(LUCell As Variant)
Dim x As Range
x = Workbooks("Book12.xlsx").Worksheets("test").Range("B2:H6")
vlook = Application.VLookup(LUCell, Worksheets("test").x, 4, False)
End Function
 

RoryA

MrExcel MVP, Moderator
Joined
May 2, 2008
Messages
36,463
Office Version
  1. 365
  2. 2019
  3. 2016
  4. 2010
Platform
  1. Windows
  2. MacOS
It's just:

Code:
vlook = Application.VLookup(LUCell, x, 4, False)

and note that x is an array, not a range object.
 

kweaver

Well-known Member
Joined
May 8, 2018
Messages
1,583
Office Version
  1. 365
  2. 2010
RoryA: I did try that but it didn't work. Do I need to save the Book12 first?

Code:
Function vlook(LUCell As Variant)
Dim x As Range
x = Workbooks("Book12.xlsx").Worksheets("test").Range("B2:H6")
vlook = Application.VLookup(LUCell, x, 4, False)
End Function

I tried saving it as Book12.xlsm and changed the UDF accordingly. Still no luck.
 

RoryA

MrExcel MVP, Moderator
Joined
May 2, 2008
Messages
36,463
Office Version
  1. 365
  2. 2019
  3. 2016
  4. 2010
Platform
  1. Windows
  2. MacOS

ADVERTISEMENT

As I mentioned, x is not a range. You should declare it as Variant.
 

Pauline10

New Member
Joined
Feb 22, 2021
Messages
9
Office Version
  1. 365
Platform
  1. Windows
In that case I think you are out of luck. I know of no way for a UDF to access a closed workbook.
If the workbook was open you would use
VBA Code:
Function Vachercher(a As Variant)

x = Workbooks("21-02-17 - FACTURES 14-21.xlsx").Worksheets("Restitution").Range("B2:BT36000")
Vachercher = Application.VLookup(a, x, 11, False)

End Function
Thank you very much, it finally works. It's a shame it can't be used with the workbook closed, but it still is very useful for me, so thank you !
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
55,549
Office Version
  1. 365
Platform
  1. Windows
Glad to help & thanks for the feedback.
 

Watch MrExcel Video

Forum statistics

Threads
1,127,588
Messages
5,625,668
Members
416,125
Latest member
NeedExcelHelp2021

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
Top