Possible to have UDF vlookup from closed workbook?

largeselection

Active Member
Joined
Aug 4, 2008
Messages
358
I'm new to UDFs, is it possible to have a UDF for a vlookup that references a closed workbook?

I have:
Code:
Function CCLookup(Rank)
 
CCLookup = excel.application.worksheetfunction.vlookup(Rank, 'O:\C\Templates\[UDF Lookup Sheet.xlsx]Sheet1'!R2C1:R676C5,2,false)
 
This is not working and I've read a few other posts that seem to suggest it's not possible to do a lookup with a closed workbook directly in vba.  Any workarounds?
 

Excel Facts

Highlight Duplicates
Home, Conditional Formatting, Highlight Cells, Duplicate records, OK to add pink formatting to any duplicates in selected range.
It's not, as such. You could use Evaluate with the formula as you would enter it. Why are you using a UDF anyway?
 
Upvote 0
I have a vlookup that I do a lot a lot a lot so I thought I might be able to simplify my life a bit by creating a UDF so that instead of always having to open the file with the references and typing in the formula I could just use a UDF like CClookup() and have it do that for me.
 
Upvote 0
How could I use evaluate and have it use the cell I target as the lookup value?

I've tried this:
Code:
Function whatnumber(target)
Dim fpath As String
Dim fname As String
Dim sname As String
fpath = "O:\C\CXXX\"
fname = "UDF LOOKUP SHEET.xlsx"
sname = "Sheet1"

whatregion = Evaluate("vlookup(target,' " & fpath & "[" & fname & "]" & sname & "'!$A$2:$E$676,2,false)")

End Function

I have also tried the similar equation, but instead of evaluate I had
Code:
Activecell.Formula = _
"=vlookup...(SAME AS ABOVE)"

Neither worked. :(
 
Upvote 0
Code:
whatregion = Evaluate("vlookup(" & target & ",' " & fpath & "[" & fname & "]" & sname & "'!$A$2:$E$676,2,false)")
 
Upvote 0
How could I use evaluate and have it use the cell I target as the lookup value?

I've tried this:
Code:
Function whatnumber(target)
Dim fpath As String
Dim fname As String
Dim sname As String
fpath = "O:\C\CXXX\"
fname = "UDF LOOKUP SHEET.xlsx"
sname = "Sheet1"

whatregion = Evaluate("vlookup(target,' " & fpath & "[" & fname & "]" & sname & "'!$A$2:$E$676,2,false)")

End Function
I have also tried the similar equation, but instead of evaluate I had
Code:
Activecell.Formula = _
"=vlookup...(SAME AS ABOVE)"
Neither worked. :(

If I understand correctly and assuming that you will use the formula just on few cells you may want to look at the "PULL" UDF: http://www.mrexcel.com/forum/showthread.php?p=2131711#post2131711
Specifically at post #4 and 5

Hope this helps

Ron
 
Upvote 0
I'm getting 0 as the result instead of "USA". Here is the code:
Code:
Function region(target)
Dim fpath As String
Dim fname As String
Dim sname As String
fpath = "O:\C\Cxxx\"
fname = "UDF LOOKUP SHEET.xlsx"
sname = "Sheet1"

region = Evaluate("=vlookup(" & target & ",' " & fpath & "[" & fname & "]" & sname & "'!$A$2:$E$676,2,false)")

End Function

Do I have to have all that code for pull before this? and then include the pull function after the vlookup?

In the alternative, I thought I might be able to use just a lot of cases so something like:
Code:
Function(Number)
 
Select Case Number.Value
 
Case "2"
ActiveCell.FormulaR1C1 = _
"USA"
 
end select
 
end function

That also didn't work, but I could perhaps use that to accomplish a similar feat since I would only have 900 cases or so.
 
Upvote 0
I got the second part to work using:
Code:
Function CClookup(number as string)
select case number
Case is = "3"
CClookup = "USA"
.
.
.
end select
 
end function
Thanks for all your help!
 
Upvote 0

Forum statistics

Threads
1,214,975
Messages
6,122,538
Members
449,088
Latest member
RandomExceller01

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