I am working on a UDF that needs to use vlookup within it. I'm having problems using a VBA variable within the vlookup function. The end goal is to have a vLookup type function that looks up each element in the cell. For example:
File 1:
Column1
1
2
3,4,5
6
File 2:
Column1-Column2
1-a
2-b
3-c
4-d
5-e
6-f
I want the file output to be
Column1- Column2
1-a
2-b
3,4,5-c,d,e
6-f
I'm trying to work on a UDF to do this but am running into a problem. Here is what I have so far (UDF is only half done but I'm stuck):
Function vLookupElement(LookupCell, Seperator, ReferenceTable As Range, ColumnIndexNumber)
Dim LookupCellArray As Variant
Dim v As String
Dim i As Integer
Dim ReturnValue
LookupCellArray = Split(LookupCell, Seperator)
ReturnValue = ""
For i = 0 To UBound(LookupCellArray)
v = LookupCellArray(i)
MsgBox (v & "-" & LookupCell)
ReturnValue = Application.WorksheetFunction.VLookup(v, ReferenceTable, ColumnIndexNumber, False)
Next i
vLookupElement = ReturnValue
End Function
When I have the vlookup formula try to access the array variable by either Application.WorksheetFunction.VLookup(v, ReferenceTable, ColumnIndexNumber, False) or Application.WorksheetFunction.VLookup(LookupCellArray(i), ReferenceTable, ColumnIndexNumber, False), nothing is found. If I modify this to Application.WorksheetFunction.VLookup(LookupCell, ReferenceTable, ColumnIndexNumber, False) it works. Since I need to look up each element in the cell, I need to feed the vlookup formula a VBA variable and not just the LookupCell value. Why isn't that working? In the msgbox both v and LookupCell appear to be the same so I'm wondering if this is a formatting issue or non-printing character issue?
Bookmarks