VBA InStr function getting partial matches - I need exact matches - PLEASE HELP

doispe

New Member
Joined
Jun 22, 2021
Messages
8
Office Version
  1. 365
Platform
  1. Windows
  2. MacOS
Hello,

I'm new to excel, started this year with the formulas and been adventuring myself in VBA to make some of my tasks easier.

I have 2 different workbooks, one holds a column that contains a list of names, and in the other workbook, there's a column holding different name mistypes that I'm trying to find among the first workbook.

I wrote this below, after searching the internet the whole day, and for my surprise it did work! Except its considering partial matches too. What I need is to find exact words instead, but I don't know how, maybe using split or something?

I thought about adding a & " " or " " & as extra condition to the substring and it did work for some cases, but still there are situations it won't work, considering there could be a " " followed by a partial match.

Could anyone help me out on this, please? I'm so close

VBA Code:
Sub Name_Mistypes()

    Dim str As Range
    Dim dummy As Range
    Dim s2 As Worksheet
    Dim i As Long

    Set s2 = Workbooks("Dummy Book.xlsx").Sheets("Sheet1")

    With s2
        i = .Cells(.Rows.Count, "B").End(xlUp).Row
        Set dummy = .Range("B2:B" & i)
    End With

    For Each cell In Selection
    For Each str In dummy.Cells

        If InStr(1, cell.Value, " " & str.Value, vbTextCompare) <> 0 Or _
           InStr(1, cell.Value, str.Value & " ", vbTextCompare) Then
        cell.Interior.Color = RGB(255, 199, 206)
        End If

        Next str
        Next cell

End Sub

PS: The " " & are included in the code above for ilustration only, as it didn't work out as a solution
 
ANTOS DA MARIA (wouldn't work because there's no " " before ANTOS)

DA MARIA ANTOS (wouldn't work because there's no " " after ANTOS)

Those cases are covered, anticipating those cases, I added to the beginning and the end " " of the value.

If InStr(1, " " & cell.Value & " "

I think all cases are covered, but if you find any, please let me know.
 
Upvote 0

Excel Facts

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying
Can't thank you enough!!! Been the whole day struggling with this, you made it look so easy hehe

Thank you very much Dante, will sure let you know if I find out any missing cases :)
 
Upvote 0

Forum statistics

Threads
1,223,885
Messages
6,175,184
Members
452,615
Latest member
bogeys2birdies

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