+ Reply to Thread
Results 1 to 8 of 8

Using vLookup in UDF

Hybrid View

  1. #1
    Registered User
    Join Date
    12-12-2014
    Location
    USA
    MS-Off Ver
    2010
    Posts
    12

    Using vLookup in UDF

    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?

  2. #2
    Forum Guru
    Join Date
    03-02-2006
    Location
    Los Angeles, Ca
    MS-Off Ver
    WinXP/MSO2007;Win10/MSO2016
    Posts
    12,629

    Re: Using vLookup in UDF

    Why not use Range.Find to get the row of v and Range.Offset to get the value in the required column in that row for the returnvlue?
    Ben Van Johnson

  3. #3
    Forum Guru bakerman2's Avatar
    Join Date
    10-03-2012
    Location
    Antwerp, Belgium
    MS-Off Ver
    MO Prof Plus 2016
    Posts
    6,909

    Re: Using vLookup in UDF

    Give this a whirl.
    Function vLookupElement(LookupCell As Range, Seperator As String, ReferenceTable As Range, ColumnIndexNumber As Long) As String
        Dim LookupCellArray As Variant
        Dim v As Long
        Dim i As Integer
        Dim ReturnValue
            
        LookupCellArray = Split(LookupCell, Seperator)
        ReturnValue = ""
        
        For i = 0 To UBound(LookupCellArray)
            v = LookupCellArray(i)
            'MsgBox (v & "-" & LookupCell)
            ReturnValue = ReturnValue & "," & Application.VLookup(v, ReferenceTable, ColumnIndexNumber, False)
            
        Next i
        
        vLookupElement = Mid(ReturnValue, 2)
    End Function
    Last edited by bakerman2; 03-02-2018 at 11:34 PM.
    Avoid using Select, Selection and Activate in your code. Use With ... End With instead.
    You can show your appreciation for those that have helped you by clicking the * at the bottom left of any of their posts.

  4. #4
    Registered User
    Join Date
    12-12-2014
    Location
    USA
    MS-Off Ver
    2010
    Posts
    12

    Re: Using vLookup in UDF

    ProtonLeah- that is an interesting want to solve the problem. I might try that.
    Bakerman2- that works, although I do need it flexible enough to work where the lookup values are not just numbers. I'm confused on why setting v to an integer instead of a string works. I'd like this as flexible enough to work like the normal v lookup where you can lookup:
    1
    2,3
    a
    a,b
    test1
    test2

    I tried making v a variant but then it gives the generic #value error.

  5. #5
    Forum Guru bakerman2's Avatar
    Join Date
    10-03-2012
    Location
    Antwerp, Belgium
    MS-Off Ver
    MO Prof Plus 2016
    Posts
    6,909

    Re: Using vLookup in UDF

    One way to go.
    Function vLookupElement(LookupCell As Range, Seperator As String, ReferenceTable As Range, ColumnIndexNumber As Long) As String
        Dim LookupCellArray As Variant
        Dim v
        Dim i As Integer
        Dim ReturnValue
            
        LookupCellArray = Split(LookupCell, Seperator)
        ReturnValue = ""
        
        For i = 0 To UBound(LookupCellArray)
            If IsNumeric(LookupCellArray(i)) Then
                v = CLng(LookupCellArray(i))
            Else
                v = LookupCellArray(i)
            End If
            'MsgBox (v & "-" & LookupCell)
            ReturnValue = ReturnValue & "," & Application.VLookup(v, ReferenceTable, ColumnIndexNumber, False)
            
        Next i
        vLookupElement = Mid(ReturnValue, 2)
    End Function

  6. #6
    Forum Expert
    Join Date
    11-28-2015
    Location
    indo
    MS-Off Ver
    2016 64 bitt
    Posts
    1,433

    Re: Using vLookup in UDF

    Not test
    Function Test(r as range,rg as range,Optional s as string="")
    Dim x,i as long,y,f
    x = split(r.value,s)
    y = rg.value: Test = ""
    For i = 0 to Ubound(x)
      f = application.match(Trim$(x(i)),rg,0)
     If not iserror f then 
        Test = iif(Test="",f,Test & "," & f)
     End if
    Next i
    End function
    Last edited by daboho; 03-04-2018 at 06:36 AM.
    "Presh Star Who has help you *For Add Reputation!! And mark case as Solve"

  7. #7
    Registered User
    Join Date
    12-12-2014
    Location
    USA
    MS-Off Ver
    2010
    Posts
    12

    Re: Using vLookup in UDF

    Thanks bakerman2. I modified it some to account for values that aren't found, but it is working pretty well now.

    Function vLookupElement(LookupCell As Range, Seperator As String, ReferenceTable As Range, ColumnIndexNumber As Long, ValueIfNotFound As String) As String
        Dim LookupCellArray As Variant
        Dim v
        Dim i As Integer
        Dim ReturnValue
            
        LookupCellArray = Split(LookupCell, Seperator)
        ReturnValue = ""
        
        For i = 0 To UBound(LookupCellArray)
            If IsNumeric(LookupCellArray(i)) Then
                v = CLng(LookupCellArray(i))
            Else
                v = LookupCellArray(i)
            End If
            'MsgBox (v & "-" & LookupCell)
            If IsError(Application.VLookup(v, ReferenceTable, ColumnIndexNumber, False)) Then
                ReturnValue = ReturnValue & Seperator & ValueIfNotFound
            Else
                ReturnValue = ReturnValue & Seperator & Application.VLookup(v, ReferenceTable, ColumnIndexNumber, False)
            End If
            
        Next i
        vLookupElement = Mid(ReturnValue, 2)
    End Function

  8. #8
    Forum Guru bakerman2's Avatar
    Join Date
    10-03-2012
    Location
    Antwerp, Belgium
    MS-Off Ver
    MO Prof Plus 2016
    Posts
    6,909

    Re: Using vLookup in UDF

    Glad to help and thanks for rep+.

    If that takes care of your original question, please select Thread Tools from the menu link above and mark this thread as SOLVED. Thanks.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Replies: 5
    Last Post: 12-07-2016, 09:18 AM
  2. Replies: 3
    Last Post: 12-04-2014, 01:27 PM
  3. Replies: 0
    Last Post: 12-26-2013, 01:12 PM
  4. Display Cell within VLookup range that excel thinks matches the VLookup Value
    By headachexcelperson in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 09-04-2013, 04:56 PM
  5. [SOLVED] Vlookup is not working and giving =vlookup(B2,$T$2:$U$135,2,false) this kind of values.
    By yogeshsharma1981 in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 07-09-2013, 04:08 PM
  6. vlookup? match? index? MULTIPLE criteria for vlookup search problem....
    By aborg88 in forum Excel Formulas & Functions
    Replies: 10
    Last Post: 02-11-2013, 09:56 AM
  7. Replies: 5
    Last Post: 07-29-2009, 07:53 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1