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
 

Excel Facts

How to calculate loan payments in Excel?
Use the PMT function: =PMT(5%/12,60,-25000) is for a $25,000 loan, 5% annual interest, 60 month loan.
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
 
Upvote 0
It's just:

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

and note that x is an array, not a range object.
 
Upvote 0
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.
 
Upvote 0
As I mentioned, x is not a range. You should declare it as Variant.
 
Upvote 0
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 !
 
Upvote 0
Glad to help & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,213,535
Messages
6,114,194
Members
448,554
Latest member
Gleisner2

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