+ Reply to Thread
Results 1 to 8 of 8

Thread: Multiple lookup function fails when called as a UDF

  1. #1
    Valued Forum Contributor
    Join Date
    07-16-2010
    Location
    Northumberland, UK
    MS-Off Ver
    Excel 2007 (home), Excel 2010 (work)
    Posts
    1,957

    Multiple lookup function fails when called as a UDF

    Morning all (it's morning in the UK, at least) - this is a problem I've come across in the past and worked around, but I wonder if one of the gurus here can tell me what the issue is.

    I have a fairly straightforward function to concatenate the results from (effectively) a VLOOKUP on a range where there are multiple matches ...

    Function MultiVLookup(vMatchCriteria As Variant, rngLookUpArea As Range, lOffset As Long, Optional sDelimiter = ",") As String
    
    Dim rngMatchValue As Range
    Dim sFirstAddress As String
    Dim sTmpReturn As String
    
    sTmpReturn = ""
    
    With rngLookUpArea
      
      Set rngMatchValue = .Find(vMatchCriteria, LookIn:=xlValues, lookat:=xlWhole, MatchCase:=False)
      
      If Not rngMatchValue Is Nothing Then
      
        sFirstAddress = rngMatchValue.Address
        
        Do
        
          sTmpReturn = sTmpReturn & rngMatchValue.Offset(0, lOffset).Value & sDelimiter
          
          Set rngMatchValue = .FindNext(rngMatchValue)
          
        Loop Until rngMatchValue.Address = sFirstAddress
        
      End If
      
    End With
    
    If Len(sTmpReturn) > 0 And Len(sDelimiter) > 0 Then
      sTmpReturn = Left(sTmpReturn, Len(sTmpReturn) - Len(sDelimiter))
    End If
    
    MultiVLookup = sTmpReturn
      
    End Function
    If I call this from a VBA subroutine it works fine, and produces the expected results:

    Sub TestCall()
    
    MsgBox MultiVLookup("A", ThisWorkbook.Sheets(1).Range("A1:A3"), 1)
    
    End Sub
    If, however, I call it as a UDF it fails. A quick bit of debugging shows that this is because the .FindNext method returns an empty range, which shouldn't be possible, as there are both more matches within the range and, even if there weren't, there must have been at least one match, so it should loop back to that.

    Things I've tried:

    1. Naming the range explicitly, rather than using a With statement - no difference.
    2. Defining another range within the function and setting it to the search range before the first search - no difference.

    However, the brainwave I had half-way through typing this post was to replace the .FindNext statement with:

    Set rngMatchValue = .Find(vMatchCriteria, LookIn:=xlValues, lookat:=xlWhole, MatchCase:=False, after:=rngMatchValue)
    And that does work as a UDF.

    So, I guess my question not isn't so much "How do I make this work" as it is "Why the hecky-thump didn't it work with .FindNext". Anybody care to enlighten me?

    Thanks.
    Last edited by Andrew-R; 02-10-2012 at 06:15 AM.

  2. #2
    Valued Forum Contributor OnErrorGoto0's Avatar
    Join Date
    12-30-2011
    Location
    I DO NOT POST HERE ANYMORE
    MS-Off Ver
    I DO NOT POST HERE ANYMORE
    Posts
    1,647

    Re: Multiple lookup function fails when called as a UDF

    I don't know why but it just doesn't. It was not until Excel 2002 that Find worked in UDFs either.
    Good luck.

  3. #3
    Valued Forum Contributor
    Join Date
    07-16-2010
    Location
    Northumberland, UK
    MS-Off Ver
    Excel 2007 (home), Excel 2010 (work)
    Posts
    1,957

    Re: Multiple lookup function fails when called as a UDF

    Just one of those things, eh? I'll mentally file it under "Excel quirks, annoying" (cf ".VPageBreaks.Count is a lying weasel")

  4. #4
    Valued Forum Contributor OnErrorGoto0's Avatar
    Join Date
    12-30-2011
    Location
    I DO NOT POST HERE ANYMORE
    MS-Off Ver
    I DO NOT POST HERE ANYMORE
    Posts
    1,647

    Re: Multiple lookup function fails when called as a UDF

    Yes. I suspect (but do not know for sure) that it is a similar reason to why properties like currentregion and currentarray do not work in UDFs.
    Good luck.

  5. #5
    Forum Guru snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,151

    Re: Multiple lookup function fails when called as a UDF

    It's probably more a conceptual thing...

    Function MultiVLookup_snb(vMatchCriteria As Variant, rngLookUpArea As Range, lOffset As Long, Optional sDelimiter = ",") As String
      MultiVLookup_snb = ""
      MultiVLookup_snb = Join(Filter(Evaluate("transpose(If(" & rngLookUpArea.Resize(, 1).Address & "=""" & vMatchCriteria & """," & rngLookUpArea.Resize(, 1).Offset(, lOffset).Address & ",""~""))"), "~", False), sDelimiter)
    End Function



  6. #6
    Valued Forum Contributor OnErrorGoto0's Avatar
    Join Date
    12-30-2011
    Location
    I DO NOT POST HERE ANYMORE
    MS-Off Ver
    I DO NOT POST HERE ANYMORE
    Posts
    1,647

    Re: Multiple lookup function fails when called as a UDF

    I confess I do not know what you mean by that.

    Oh, and that function won't lookup numbers.
    Last edited by OnErrorGoto0; 02-10-2012 at 07:12 AM.
    Good luck.

  7. #7
    Forum Guru snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,151

    Re: Multiple lookup function fails when called as a UDF

    Function MultiVLookup_snb(vMatchCriteria As Variant, rngLookUpArea As Range, lOffset As Long, Optional sDelimiter = ",") As String
      MultiVLookup_snb = ""
      If Not IsNumeric(vMatchCriteria) Then vMatchCriteria = Chr(34) & vMatchCriteria & Chr(34)
      MultiVLookup_snb = Join(Filter(Evaluate("transpose(If(" & rngLookUpArea.Resize(, 1).Address & "=" & vMatchCriteria & "," & rngLookUpArea.Resize(, 1).Offset(, lOffset).Address & ",""~""))"), "~", False), sDelimiter)
    End Function



  8. #8
    Valued Forum Contributor OnErrorGoto0's Avatar
    Join Date
    12-30-2011
    Location
    I DO NOT POST HERE ANYMORE
    MS-Off Ver
    I DO NOT POST HERE ANYMORE
    Posts
    1,647

    Re: Multiple lookup function fails when called as a UDF

    That one won't lookup numbers stored as text.
    Good luck.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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.2.0