Compare a single name text string to a string with multiple names

CBrowne

New Member
Joined
Mar 18, 2024
Messages
4
Office Version
  1. 2021
Platform
  1. Windows
I'm still fairly new to Excel VBA and I'm hoping this is a simple problem?
I have an excel worksheet with hundreds of people's last names.
I've provided the user with the ability to enter a last name and the system will search the worksheet and list all the entries with matching last name.
That works. However, when the last name in the worksheet has multiple names (from re-marriages, etc), it doesn't work.

An example of the worksheet:

COLUMN A (1) COLUMN B (2)
LAST NAME FIRST NAME

Brown John
Smith David
Churchill Jim
Sturr Churchill Barber Laura
King Daniel
Church Susan
Churchill Bill

The user enters the name they want in a userform into "TextBoxLastName"

My vba code loops thru the entire worksheet using the following code to find the matching last names:

If StrComp(Cells(RowNo, 1).Value, TextBoxLastName.Value) = 0 then
do stuff and list the person in a report

User enters Churchill in the TextBoxLastName.

The results I get are:
Churchill Jim
Churchill Bill

How do I change the String Compare to also pick up the person with last name = Sturr Churchill Barber?

Any help would be greatly appreciated!

Charlene
 
Try this line instead
VBA Code:
If InStr(1, " " & Cells(RowNo, 1).Value & " ", " " & TextBoxLastName.Value & " ", vbTextCompare) > 0 Then
 
Upvote 0
Solution
If InStr(1, " " & Cells(RowNo, 1).Value & " ", " " & TextBoxLastName.Value & " ", vbTextCompare) > 0 Then
Thank you Peter, it worked perfectly. Now I'll have to study-up on the solution and tuck it into my brain! Thanks for the learning experience too!
 
Upvote 0

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