+ Reply to Thread
Results 1 to 16 of 16

VLOOKUP Closest Match Not Close Enough

  1. #1
    Ronster
    Guest

    VLOOKUP Closest Match Not Close Enough

    I'm comparing one string array with another using VLOOKUP. About half
    the list matches exactly if I use FALSE in the Range_Lookup. On the
    items that don't match I used TRUE in the Range_Lookup but found it
    doesn't get the closest match. See example:

    Test Table Array
    AAABBBCCC
    AAACCCCCC
    AAACCCDDD

    Lookup Value = AAACCCCC

    VLOOKUP returns 1st item in Table (AAABBBCCC) ????????????

    Of course item 2 (AAACCCCCC) is the better match but VLOOKUP doesn't
    see it that way.

    Does anyone know of a way around this problem? Maybe a character by
    character match function that returns the item that matchs the most
    number of correct characters?

    Thanks in advance.


  2. #2
    Jim Thomlinson
    Guest

    RE: VLOOKUP Closest Match Not Close Enough

    When you use true, the list that your are looking up into must be sorted,
    otherwise it may return the wrong value.
    --
    HTH...

    Jim Thomlinson


    "Ronster" wrote:

    > I'm comparing one string array with another using VLOOKUP. About half
    > the list matches exactly if I use FALSE in the Range_Lookup. On the
    > items that don't match I used TRUE in the Range_Lookup but found it
    > doesn't get the closest match. See example:
    >
    > Test Table Array
    > AAABBBCCC
    > AAACCCCCC
    > AAACCCDDD
    >
    > Lookup Value = AAACCCCC
    >
    > VLOOKUP returns 1st item in Table (AAABBBCCC) ????????????
    >
    > Of course item 2 (AAACCCCCC) is the better match but VLOOKUP doesn't
    > see it that way.
    >
    > Does anyone know of a way around this problem? Maybe a character by
    > character match function that returns the item that matchs the most
    > number of correct characters?
    >
    > Thanks in advance.
    >
    >


  3. #3
    Niek Otten
    Guest

    Re: VLOOKUP Closest Match Not Close Enough

    AAACCCCCC is greater than AAACCCC so Excel returns the one before.
    If there is not too much variation in the length of the string you could
    introduce some extra columns in your table, derived from the original one,
    wit fewer characters.
    In your formula you could choose the column to look in, depending on the
    length of your search argument.
    To find out the length of your search argument: =LEN(A2)
    To get a smaller column: =LEFT(A1,LEN(A1)-1)

    --
    Kind regards,

    Niek Otten


    "Ronster" <[email protected]> wrote in message
    news:[email protected]...
    > I'm comparing one string array with another using VLOOKUP. About half
    > the list matches exactly if I use FALSE in the Range_Lookup. On the
    > items that don't match I used TRUE in the Range_Lookup but found it
    > doesn't get the closest match. See example:
    >
    > Test Table Array
    > AAABBBCCC
    > AAACCCCCC
    > AAACCCDDD
    >
    > Lookup Value = AAACCCCC
    >
    > VLOOKUP returns 1st item in Table (AAABBBCCC) ????????????
    >
    > Of course item 2 (AAACCCCCC) is the better match but VLOOKUP doesn't
    > see it that way.
    >
    > Does anyone know of a way around this problem? Maybe a character by
    > character match function that returns the item that matchs the most
    > number of correct characters?
    >
    > Thanks in advance.
    >




  4. #4
    Charlie
    Guest

    RE: VLOOKUP Closest Match Not Close Enough

    The list is sorted, but the lookup value has one less "C", therefore, having
    fewer characters than AAABBBCCC makes it return the first value in the lookup
    list, no?

    "Jim Thomlinson" wrote:

    > When you use true, the list that your are looking up into must be sorted,
    > otherwise it may return the wrong value.
    > --
    > HTH...
    >
    > Jim Thomlinson
    >
    >
    > "Ronster" wrote:
    >
    > > I'm comparing one string array with another using VLOOKUP. About half
    > > the list matches exactly if I use FALSE in the Range_Lookup. On the
    > > items that don't match I used TRUE in the Range_Lookup but found it
    > > doesn't get the closest match. See example:
    > >
    > > Test Table Array
    > > AAABBBCCC
    > > AAACCCCCC
    > > AAACCCDDD
    > >
    > > Lookup Value = AAACCCCC
    > >
    > > VLOOKUP returns 1st item in Table (AAABBBCCC) ????????????
    > >
    > > Of course item 2 (AAACCCCCC) is the better match but VLOOKUP doesn't
    > > see it that way.
    > >
    > > Does anyone know of a way around this problem? Maybe a character by
    > > character match function that returns the item that matchs the most
    > > number of correct characters?
    > >
    > > Thanks in advance.
    > >
    > >


  5. #5
    Ronster
    Guest

    Re: VLOOKUP Closest Match Not Close Enough

    Are any of you aware of a function that does a character by character
    match of a string to an array no matter what the length of the string?
    If I don't get an exact match using VLOOKUP what I really want is the
    item that matches the most number of characters in the string.


  6. #6
    Niek Otten
    Guest

    Re: VLOOKUP Closest Match Not Close Enough

    I think I gave you a start. Post back if that didn't help you enough

    --
    Kind regards,

    Niek Otten

    "Ronster" <[email protected]> wrote in message
    news:[email protected]...
    > Are any of you aware of a function that does a character by character
    > match of a string to an array no matter what the length of the string?
    > If I don't get an exact match using VLOOKUP what I really want is the
    > item that matches the most number of characters in the string.
    >




  7. #7
    Ronster
    Guest

    Re: VLOOKUP Closest Match Not Close Enough

    Thanks Niek but I should have mentioned before my seach strings vary
    from 4 to 52 chars and that's just in list I'm presently using. Other
    list I need to search may be more or less so creating a column for each
    search length gets a little large. Also in some search strings only a
    few characters actually match anything in the table array so the seach
    string could be 45 characters but only the first 3 characters match to
    something in the table array. I think a function would work better.


  8. #8
    Jim Thomlinson
    Guest

    RE: VLOOKUP Closest Match Not Close Enough

    Sorry about that I thought they were the same length... You will need to
    parse the strings into smaller strings (as per Niek's suggestion) and play
    around to get what you want. This is one of those cases where what appears to
    be the simplest thing is the most difficult to accomplish.
    --
    HTH...

    Jim Thomlinson


    "Charlie" wrote:

    > The list is sorted, but the lookup value has one less "C", therefore, having
    > fewer characters than AAABBBCCC makes it return the first value in the lookup
    > list, no?
    >
    > "Jim Thomlinson" wrote:
    >
    > > When you use true, the list that your are looking up into must be sorted,
    > > otherwise it may return the wrong value.
    > > --
    > > HTH...
    > >
    > > Jim Thomlinson
    > >
    > >
    > > "Ronster" wrote:
    > >
    > > > I'm comparing one string array with another using VLOOKUP. About half
    > > > the list matches exactly if I use FALSE in the Range_Lookup. On the
    > > > items that don't match I used TRUE in the Range_Lookup but found it
    > > > doesn't get the closest match. See example:
    > > >
    > > > Test Table Array
    > > > AAABBBCCC
    > > > AAACCCCCC
    > > > AAACCCDDD
    > > >
    > > > Lookup Value = AAACCCCC
    > > >
    > > > VLOOKUP returns 1st item in Table (AAABBBCCC) ????????????
    > > >
    > > > Of course item 2 (AAACCCCCC) is the better match but VLOOKUP doesn't
    > > > see it that way.
    > > >
    > > > Does anyone know of a way around this problem? Maybe a character by
    > > > character match function that returns the item that matchs the most
    > > > number of correct characters?
    > > >
    > > > Thanks in advance.
    > > >
    > > >


  9. #9
    Jim Thomlinson
    Guest

    Re: VLOOKUP Closest Match Not Close Enough

    The logic on this is very ugly because it is fuzzy. To come up with one
    function that fits all of the possible circumstances will be very difficult.
    For example what to do if we are trying to match a 7 character string to
    items in the list and:

    One possible match has all 7 characters but not in a row.
    Another possible matches 6 charaters starting in the second position
    Another possible matches the first 5 characters exactly
    Another matches the last 5 characters exactly

    Which ONE should the function return??? There needs to be very definite
    rules in place that work for ALL cases.
    --
    HTH...

    Jim Thomlinson


    "Ronster" wrote:

    > Thanks Niek but I should have mentioned before my seach strings vary
    > from 4 to 52 chars and that's just in list I'm presently using. Other
    > list I need to search may be more or less so creating a column for each
    > search length gets a little large. Also in some search strings only a
    > few characters actually match anything in the table array so the seach
    > string could be 45 characters but only the first 3 characters match to
    > something in the table array. I think a function would work better.
    >
    >


  10. #10
    Kleev
    Guest

    Re: VLOOKUP Closest Match Not Close Enough

    No warranties, but this function appears to do what you are asking (based on
    the tests I threw at it.)

    Function MatchChar(myString As String, myRng As Range) As Variant
    Dim L As Integer, L2 As Integer
    Dim i As Integer
    Dim cell As Range
    Dim TestChar As String, CellVal As String, CellChar As String
    Dim NotFound As Boolean
    Dim MatchCnt As Integer, PrevMatchCnt As Integer
    Dim MatchRowIndex As Long
    Dim RowIndex As Integer

    PrevMatchCnt = 0
    RowIndex = 0
    L = Len(myString)
    If myString = "" Then
    MatchChar = CVErr(2042)
    Else
    For Each cell In myRng
    MatchCnt = 0
    RowIndex = RowIndex + 1
    NotFound = False
    CellVal = cell.Value
    L2 = Len(CellVal)
    i = 1
    Do Until i > L Or i > L2 Or NotFound
    TestChar = Mid(myString, i, 1)
    CellChar = Mid(CellVal, i, 1)
    If TestChar = CellChar Then
    MatchCnt = MatchCnt + 1
    Else
    NotFound = True
    End If
    i = i + 1
    Loop
    If MatchCnt > PrevMatchCnt Then
    MatchRowIndex = RowIndex
    PrevMatchCnt = MatchCnt
    End If
    Next cell
    If PrevMatchCnt > 0 Then
    MatchChar = MatchRowIndex
    Else
    MatchChar = CVErr(2042)
    End If
    End If
    End Function

    Here is some data I threw at it. Data range is A2:B6. Formula in C2
    (copied down through C11) is: =MatchChar(D2,$A$2:$A$6)
    Using the function with the Index function for rows 12 - 22 (starting from
    where the word Index -----------> is) formula is:
    =INDEX($A$2:$B$6,MatchChar(D12,$A$2:$A$6),2)

    AAABBBCCC B2 2 AAACCCCC
    AAACCCCCC B3 5 mou
    AAACCCDDD B4 #N/A
    BBBABCX B5 1 AAABB
    mouse B6 2 AAACCCCC
    4 B
    1 AAAB
    1 A
    3 AAACCCD
    #N/A aaa
    Index ---------------> B2 AAA
    B3 AAACCCCC
    B6 mou
    #N/A
    B2 AAABB
    B3 AAACCCCC
    B5 B
    B2 AAAB
    B2 A
    B4 AAACCCD
    #N/A aaa


    "Ronster" wrote:

    > Are any of you aware of a function that does a character by character
    > match of a string to an array no matter what the length of the string?
    > If I don't get an exact match using VLOOKUP what I really want is the
    > item that matches the most number of characters in the string.
    >
    >


  11. #11
    Ronster
    Guest

    Re: VLOOKUP Closest Match Not Close Enough

    Thanks Kleev. Here's something I threw together and it seems to work
    pretty well.

    Function ExactCharMatch(MySearchStr As String, Rng As Range) As String
    ' Function does a character by character search to determine which
    ' range item matches the most search characters from left to right.
    ' With range sorted in ascending order only one pass is needed to
    determine best match.
    ' Use as-is or change as needed.

    Dim MyRange As Range, MySearchStrLen As Integer, I As Integer, CharPos
    As Integer
    Dim LastGoodMatch As String

    MySearchStrLen = Len(MySearchStr)

    CharPos = 1

    For Each MyRange In Rng
    Do While Left(MySearchStr, CharPos) = Left(MyRange, CharPos)
    'MsgBox Left(MySearchStr, CharPos) + " = " + Left(MyRange,
    CharPos)
    If CharPos >= MySearchStrLen Then
    Exit For
    End If
    CharPos = CharPos + 1
    LastGoodMatch = MyRange
    Loop
    If Left(MySearchStr, CharPos - 1) <> Left(MyRange, CharPos - 1)
    Then
    Exit For
    End If
    Next

    ExactCharMatch = LastGoodMatch

    End Function


  12. #12
    Kleev
    Guest

    Re: VLOOKUP Closest Match Not Close Enough

    Glad to hear you got what you needed working for you.

    "Ronster" wrote:

    > Thanks Kleev. Here's something I threw together and it seems to work
    > pretty well.
    >
    > Function ExactCharMatch(MySearchStr As String, Rng As Range) As String
    > ' Function does a character by character search to determine which
    > ' range item matches the most search characters from left to right.
    > ' With range sorted in ascending order only one pass is needed to
    > determine best match.
    > ' Use as-is or change as needed.
    >
    > Dim MyRange As Range, MySearchStrLen As Integer, I As Integer, CharPos
    > As Integer
    > Dim LastGoodMatch As String
    >
    > MySearchStrLen = Len(MySearchStr)
    >
    > CharPos = 1
    >
    > For Each MyRange In Rng
    > Do While Left(MySearchStr, CharPos) = Left(MyRange, CharPos)
    > 'MsgBox Left(MySearchStr, CharPos) + " = " + Left(MyRange,
    > CharPos)
    > If CharPos >= MySearchStrLen Then
    > Exit For
    > End If
    > CharPos = CharPos + 1
    > LastGoodMatch = MyRange
    > Loop
    > If Left(MySearchStr, CharPos - 1) <> Left(MyRange, CharPos - 1)
    > Then
    > Exit For
    > End If
    > Next
    >
    > ExactCharMatch = LastGoodMatch
    >
    > End Function
    >
    >


  13. #13
    Registered User
    Join Date
    05-06-2008
    Posts
    2

    Excel VBA Modified vlookup with closest text match

    Vlookup does a poor job with non-exact text matches.

    Prior posts do a great job of finding exact text matches, but I decided to write one with closest text match.

    Note, the optional range_lookup paramater does not do anything. Perhaps will implement that in the future to require exact match.

    Works on my machine, but usual caveat of no guarantees...

    Please Login or Register  to view this content.
    Last edited by shg; 05-07-2008 at 01:15 PM. Reason: add code tags

  14. #14
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2010
    Posts
    40,678
    bestresearch, please read the Forum Rules and then wrap your code with Code Tags.
    Last edited by shg; 05-06-2008 at 04:50 PM.

  15. #15
    Registered User
    Join Date
    05-06-2008
    Posts
    2

    Thanks. Will use Code Tags next time...

    Do I need to fix the existing post? Is there a way to do that?

  16. #16
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2010
    Posts
    40,678
    Probably not, at this point -- I'll edit your post.

+ 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.6.0 RC 1