First & Last Name in a Single Cell Comparison Checker

censo

Board Regular
Joined
Dec 25, 2015
Messages
161
Office Version
  1. 2013
Platform
  1. Windows
Greetings,

I have two columns (A:B) with First and Last names present in each column and I'd like to construct a function in column C that compares the entire cell contents from B:A and only indicates when it cannot discern a match for a given row.

Additional Details

- I do not require the names be parsed out into their own columns.
- If either the first or last name is detected, the result is considered a match
- If neither first or last name is detected, the result is considered a mismatch and outputs an "N"
- Middle names or initials are sometimes present. Middle names can be evaluated but initials should be ignored (so, any characters >1)
- The key cell is B, to which A will be compared for determining a mismatch (B-->A)

Basically, if any component of a name can be detected (except a single character Middle Initial), it is considered a match and does nothing (blank value)

Diagram below shows intended output

If the below is not possible, I'd entertain the next best solution that gets me close.

Thanks in advance.

- C

Book1
ABC
1Name AName BMatch?
2Sara LeeSara Lee
3S LeeSara Lee
4Sara M LeeS M Lee
5Sara LS LeeN
Sheet1
 

Excel Facts

Why are there 1,048,576 rows in Excel?
The Excel team increased the size of the grid in 2007. There are 2^20 rows and 2^14 columns for a total of 17 billion cells.
I haven't figured out handling the middle name or initial, but I think this does the first/last name match or not.

continents.xlsx
ABC
1Name AName BMatch?
2Sara LeeSara LeeY
3S LeeSara LeeY
4Sara M LeeS M LeeY
5Sara LS LeeN
Sheet1
Cell Formulas
RangeFormula
C2:C5C2=IF(OR(LEFT(FIND(" ",A2)-1)=LEFT(FIND(" ",B2)-1),(RIGHT(A2,LEN(A2)-FIND("@",SUBSTITUTE(A2," ","@",LEN(A2)-LEN(SUBSTITUTE(A2," ",""))),1)))=(RIGHT(B2,LEN(B2)-FIND("@",SUBSTITUTE(B2," ","@",LEN(B2)-LEN(SUBSTITUTE(B2," ",""))),1)))),"Y","N")
 
Upvote 0
Another approach...

VBA Code:
Function Compare(nameB As Range, nameA As Range) As String
Dim arrB As Variant, arrA As Variant
Dim i As Long, j As Long

arrB = Split(nameB.Value)
arrA = Split(nameA.Value)
For i = 0 To UBound(arrB)
    For j = 0 To UBound(arrA)
        If arrB(i) = arrA(j) And Len(arrB(i)) > 1 Then
            Compare = ""
        Else
            Compare = "N"
        End If
    Next j
Next i
End Function

Happy Holidaze!

Tony
 
Upvote 0
Book1
ABC
1Name AName BMatch?
2Sara LeeSara LeeY
3S LeeSara LeeY
4Sara M LeeS M LeeY
5Sara LS LeeN
Sheet1
Cell Formulas
RangeFormula
C2:C5C2=IF(COUNTIF(B2,SUBSTITUTE(A2," ","*"))+COUNTIF(A2,SUBSTITUTE(B2," ","*")),"Y","N")
 
Upvote 0
I haven't figured out handling the middle name or initial, but I think this does the first/last name match or not.

continents.xlsx
ABC
1Name AName BMatch?
2Sara LeeSara LeeY
3S LeeSara LeeY
4Sara M LeeS M LeeY
5Sara LS LeeN
Sheet1
Cell Formulas
RangeFormula
C2:C5C2=IF(OR(LEFT(FIND(" ",A2)-1)=LEFT(FIND(" ",B2)-1),(RIGHT(A2,LEN(A2)-FIND("@",SUBSTITUTE(A2," ","@",LEN(A2)-LEN(SUBSTITUTE(A2," ",""))),1)))=(RIGHT(B2,LEN(B2)-FIND("@",SUBSTITUTE(B2," ","@",LEN(B2)-LEN(SUBSTITUTE(B2," ",""))),1)))),"Y","N")

Thanks for your prompt reply.

As you alluded to, whenever a single name or just an initial is detected, I receive the #VALUE! error


Book1
ABC
1Name AName BMatch?
2Sara LeeSara LeeY
3LeeSara Lee#VALUE!
4Sara M LeeS M LeeY
5Sara LS#VALUE!
Sheet2
Cell Formulas
RangeFormula
C2:C5C2=IF(OR(LEFT(FIND(" ",A2)-1)=LEFT(FIND(" ",B2)-1),(RIGHT(A2,LEN(A2)-FIND("@",SUBSTITUTE(A2," ","@",LEN(A2)-LEN(SUBSTITUTE(A2," ",""))),1)))=(RIGHT(B2,LEN(B2)-FIND("@",SUBSTITUTE(B2," ","@",LEN(B2)-LEN(SUBSTITUTE(B2," ",""))),1)))),"Y","N")
 
Upvote 0
Book1
ABC
1Name AName BMatch?
2Sara LeeSara LeeY
3S LeeSara LeeY
4Sara M LeeS M LeeY
5Sara LS LeeN
Sheet1
Cell Formulas
RangeFormula
C2:C5C2=IF(COUNTIF(B2,SUBSTITUTE(A2," ","*"))+COUNTIF(A2,SUBSTITUTE(B2," ","*")),"Y","N")
Thanks for your contribution

I observed an anomaly where if just a single name is detected, the function returns a false positive (e.g., Row 2 below should be a match because even though the last name "Lee" is not present in Column A, there is a successful match of "Sara" found in both cells.

Can a slight modification to your function achieve the desired outcome?

Book1
ABC
1Name AName BMatch?
2SaraSara LeeN
3S LeeSara LeeY
4Sara M LeeS M LeeY
5Sara LS LeeN
Sheet1
Cell Formulas
RangeFormula
C2:C5C2=IF(COUNTIF(B2,SUBSTITUTE(A2," ","*"))+COUNTIF(A2,SUBSTITUTE(B2," ","*")),"Y","N")
 
Upvote 0
Another approach...

VBA Code:
Function Compare(nameB As Range, nameA As Range) As String
Dim arrB As Variant, arrA As Variant
Dim i As Long, j As Long

arrB = Split(nameB.Value)
arrA = Split(nameA.Value)
For i = 0 To UBound(arrB)
    For j = 0 To UBound(arrA)
        If arrB(i) = arrA(j) And Len(arrB(i)) > 1 Then
            Compare = ""
        Else
            Compare = "N"
        End If
    Next j
Next i
End Function

Happy Holidaze!

Tony
Thanks for your contribution, Tony. Nice play on words ... I saw what you did there (e.g., "Holidaze") :sneaky:

I would love to try out your proposed solution; however, I provided a simple spreadsheet as an example and I'm not experienced enough in VB to know how to modify the code to fit into my real-world environment.

Therefore, if you can modify your code based on the below parameters, it would be much appreciated.

Relative to the table I originally posted:

B = AU (main data column)
A = AO (column to compare against main data column, aka AU -->AO)
C = BF (comparison result output column, aka "N")

Please note I do not wish an output of "Y" if a successful match is made. Successful matches do nothing (return blank value). I only wish to evaluate the "N" results

Thanks
 
Upvote 0
Book1
ABC
1Name AName BMatch?
2SaraSara LeeY
3S LeeSara LeeY
4Sara M LeeS M LeeY
5Sara LS LeeN
Sheet1
Cell Formulas
RangeFormula
C2:C5C2=IF(COUNTIF(B2,SUBSTITUTE(A2," ","*"))+COUNTIF(A2,SUBSTITUTE(B2," ","*"))+ISNUMBER(FIND(A2,B2))+ISNUMBER(FIND(B2,A2)),"Y","N")
 
Upvote 0
Solution
"I'd like to construct a function..."

Well, I took you literally when you stated you wanted to "construct a function". The code provided does just that, and does not need to be modified to accommodate your real-world environment.

If you need guidance on how to copy/paste the code into your workbook, please reference How to Add Code to Excel Workbook and click on the Regular Module link.

To use the Compare function, select a cell in Column BF - I imagine BF2 would be appropriate - then click the Insert Function tool (to the left of the Formula Bar). Choose "User Defined" from the drop down menu next to the prompt "Or select a category:" Select the Compare function and click OK. A dialog box will appear where you can select two cells to compare - from Column AU and Column AO. Click OK and the code will return a blank cell if there's a match and an N if there's no match. Like any Excel function, you can copy/paste or click/drag the function in BF2 down the remainder of the column.
 
Upvote 0
A modification is required to accommodate the case of a single name...

VBA Code:
Function Compare(nameB As Range, nameA As Range) As String
Dim arrB As Variant, arrA As Variant
Dim i As Long, j As Long

arrB = Split(nameB.Value)
arrA = Split(nameA.Value)
For i = 0 To UBound(arrB)
    For j = 0 To UBound(arrA)
        If arrB(i) = arrA(j) And Len(arrB(i)) > 1 Then
            Compare = ""
            Exit Function
        Else
            Compare = "N"
        End If
    Next j
Next i
End Function
 
Upvote 0

Forum statistics

Threads
1,223,244
Messages
6,170,976
Members
452,372
Latest member
Natalie18

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