0

I'm attempting to create a User Defined Function to vlookup into a closed workbook on my machine. The below function works while testing it in VBA, but I get the #VALUE error in Excel when attempting to use the function. Any ideas on this? I believe I may be able to use the VBA Evaluate function to help, but so far have had no luck.

Function CUSIP_Deal_Map(CUSIP As String, DataField As String) As Variant

Dim colIndex As Integer ' for vlookup
Dim invalidDataField As Boolean
invalidDataField = False

' Switch statement, to transform from a "DataField" into a column number to be used in VLookUp

Select Case DataField
   Case "Deal"
      colIndex = 2
   Case "Class"
      colIndex = 5
   Case "DealNum"
      colIndex = 6
   Case "Vintage"
      colIndex = 11
   Case "Pool"
      colIndex = 12
   Case "Index"
      colIndex = 13
   Case Else
      invalidDataField = True
   End Select

'Dim wbk As Workbook
Set wbk = Workbooks.Open("C:\CUSIP_Map.xlsx") 'hard code location

Dim VLU_data As Variant
VLU_data = wbk.Application.WorksheetFunction.VLookup(CUSIP, Worksheets("CUSIP_Map").Range("A:M"), colIndex, False) 'vlookup data from "database"

Call wbk.Close(False) 'close connection

' Return data
If invalidDataField Then
    CUSIP_Deal_Map = "Invalid DataField"
Else
    CUSIP_Deal_Map = VLU_data
End If

End Function

The intended use in Excel would be to utilize a formula like =CUSIP_Deal_Map("123ABC","Deal")

I can test this in VBA, using this code, which returns the value I'm expecting:

Sub test()
MsgBox CUSIP_Deal_Map("123ABC", "Deal")
End Sub

Still, this doesn't work within Excel itself. I found a "pull" UDF online which seems to do something similar, but have been unsuccessful modifying it for my purposes.

2
  • Are those Select Case values the column header labels across row 1 of the closed CUSIP_Map worksheet?
    – user4039065
    May 21, 2015 at 15:33
  • @Jeeped Yes, these are the column headers
    – ch-pub
    May 21, 2015 at 15:40

2 Answers 2

2

That is because a UDF() call from within a Sub can open a file, but the same UDF() called from a worksheet cell cannot.

EDIT#1:

  • insure the UDF is in a standard module.
  • at the very top of the module include Public wbk as Workbook
  • create a workbook Open event macro in your workbook code area to open the secondary workbook and initialize wbk
6
  • Is there some work-around though? For example, I've tried to modify this pull UDF (posted numbermonger.com/2012/02/11/…) which is designed to access data from a closed workbook, but have so far been unsuccessful.
    – ch-pub
    May 21, 2015 at 15:25
  • 1
    I would open the workbook outside of the UDF() May 21, 2015 at 15:26
  • Sorry, could you please post an example? I'm somewhat new to VBA.
    – ch-pub
    May 21, 2015 at 15:29
  • "create a workbook Open event macro in your workbook code area to open the secondary workbook and initialize wbk" Is that not what I'm doing with the following code? Set wbk = Workbooks.Open("C:\CUSIP_Map.xlsx")
    – ch-pub
    May 21, 2015 at 15:40
  • YES!! but you must move that line of code from the UDF() to the Event macro!! May 21, 2015 at 15:46
0

OK, with the limitation already brokered by Gary's Student, you may want to rethink the idea of a UDF altogether. With those values from the Select Case statement in the first row of the closed CUSIP_Map worksheet, either of these standard worksheet formulas will do.

=VLOOKUP(A1, 'C:\[CUSIP_Map.xlsx]CUSIP_Map'!$A:$M, MATCH("Vintage", 'C:\[CUSIP_Map.xlsx]CUSIP_Map'!$1:$1, 0), FALSE)

=VLOOKUP(A1, 'C:\[CUSIP_Map.xlsx]CUSIP_Map'!$A:$M, LOOKUP("Class", {"Class","Deal","DealNum","Index","Pool","Vintage"}, {5,2,6,13,12,11}), FALSE)

A1 would be value to look up in the CUSIP_Map's column A. Instead of a VBA select case, the column to return is determined by either a MATCH function of the first row column headers or a hard-coded LOOKUP function of text and column numbers. Note that the LOOKUP has its values in ascending order and that it may not have as much error control as the MATCH as it will attempt partial matches. An IFERROR function as a wrapper can return "Invalid DataField" on MATCH errors.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.