Alter code to find exact or nearest match to work on string left of first comma

capson

Board Regular
Joined
Jul 9, 2010
Messages
107
Hello

I got this from Here
[h=2]vba code to find exact or nearest match and return that match in new column[/h]
It works very well, but I need for it to match only on the full name e.g

txt string = Bob Boberson, Ph.d , XYZ

r in rng = Bob Boberson, LLC

To match only on "Bob Boberson" i.e to match only on Full Names sans titles otherwise matching can be off

I tried but beyond me

Thank you for any help on this

Code:
[COLOR=blue]Function[/COLOR] VLookLike(txt [COLOR=blue]As[/COLOR] [COLOR=blue]String[/COLOR], rng [COLOR=blue]As[/COLOR] Range) [COLOR=blue]As[/COLOR] [COLOR=blue]String[/COLOR] 
    [COLOR=blue]Dim[/COLOR] temp [COLOR=blue]As[/COLOR] [COLOR=blue]String[/COLOR], e, n [COLOR=blue]As[/COLOR] [COLOR=blue]Long[/COLOR], a() 
    Static RegX [COLOR=blue]As[/COLOR] [COLOR=blue]Object[/COLOR] 
    [COLOR=blue]If[/COLOR] RegX [COLOR=blue]Is[/COLOR] [COLOR=blue]Nothing[/COLOR] [COLOR=blue]Then[/COLOR] 
        [COLOR=blue]Set[/COLOR] RegX = CreateObject("VBScript.RegExp") 
        [COLOR=blue]With[/COLOR] RegX 
            .Global = [COLOR=blue]True[/COLOR] 
            .IgnoreCase = [COLOR=blue]True[/COLOR] 
            .Pattern = "(\S+).*" & Chr(2) & ".*\1" 
        [COLOR=blue]End With[/COLOR] 
    [COLOR=blue]End[/COLOR] [COLOR=blue]If[/COLOR] 
    [COLOR=blue]With[/COLOR] RegX 
        [COLOR=blue]For Each[/COLOR] e [COLOR=blue]In[/COLOR] rng.Value 
            [COLOR=blue]If[/COLOR] UCase$(e) = UCase(txt) [COLOR=blue]Then[/COLOR] 
                VLookLike = e 
                Exit [COLOR=blue]For[/COLOR] 
            [COLOR=blue]End[/COLOR] [COLOR=blue]If[/COLOR] 
            temp = Join$(Array(e, txt), Chr(2)) 
            [COLOR=blue]If[/COLOR] .test(temp) [COLOR=blue]Then[/COLOR] 
                n = n + 1 
                [COLOR=blue]Redim[/COLOR] [COLOR=blue]Preserve[/COLOR] a(1 [COLOR=blue]To[/COLOR] 2, 1 [COLOR=blue]To[/COLOR] n) 
                a(2, n) = e 
                [COLOR=blue]Do[/COLOR] [COLOR=blue]While[/COLOR] .test(temp) 
                    a(1, n) = a(1, n) + Len(.Execute(temp)(0).submatches(0)) 
                    temp = Replace(temp, .Execute(temp)(0).submatches(0), "") 
                [COLOR=blue]Loop[/COLOR] 
            [COLOR=blue]End[/COLOR] [COLOR=blue]If[/COLOR] 
        [COLOR=blue]Next[/COLOR] 
    [COLOR=blue]End With[/COLOR] 
    [COLOR=blue]If[/COLOR] (VLookLike = "") * (n > 0) [COLOR=blue]Then[/COLOR] 
        [COLOR=blue]With[/COLOR] Application 
            VLookLike = .HLookup(.Max(.Index(a, 1, 0)), a, 2, [COLOR=blue]False[/COLOR]) 
        [COLOR=blue]End With[/COLOR] 
    [COLOR=blue]End[/COLOR] [COLOR=blue]If[/COLOR] 
[COLOR=blue]End Function[/COLOR]
 

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.
I got it

I use this to get everything to the left of the ", "
Code:
Function splitCell(strValue As String, delim As String) As String
   
   If strValue <> "" Then
     splitCell = Split(strValue, delim)(0)
   Else
     strValue = ""
   End If
   
End Function

I changed this
Code:
[COLOR=blue]If[/COLOR][COLOR=#333333] UCase$(e) = UCase(txt)[/COLOR]

to this
Code:
If UCase$(splitCell(CStr(e), ", ")) = UCase(splitCell(txt, ", "))

and I get a much better match on Full Names
 
Last edited:
Upvote 0
You have found already a solution.

I have maybe found a pattern: """[^""]*""|[^,]*"
 
Upvote 0

Forum statistics

Threads
1,223,984
Messages
6,175,786
Members
452,670
Latest member
nogarth

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top