finding exact text.

CRogers

New Member
Joined
Jul 10, 2024
Messages
6
Office Version
  1. 2021
Platform
  1. Windows
Hello,

I have a spreadsheet an old colleague created where there is a macro that looks for key words. What I'm trying to do is fix it so that only catches exact words. i.e. I'm looking for "airman" and it picks up "chairman". I have the list of key words. Question : is there a way that I can make it only highlight if it says "airman"?

Thanks in advance for any help!
 
Based on your original code.
Code:
Sub test()
    Dim x, mycolor, i&, n&, r As Range, m As Object
    Application.ScreenUpdating = False
    Columns(1).Font.ColorIndex = xlAutomatic
    Columns(1).Font.Bold = False
    mycolor = Array(255, 16711680, 16746752, 16747007, 35072, 16771707, _
                48383, 48300, 8406527, 16762537)
    x = Filter([transpose(if(i1:i10000<>"",i1:i10000))], False, 0)
    With CreateObject("VBScript.RegExp")
        .Global = True
        .Pattern = "([$()^|\\\[\]{}+*?-])"
        For i = 0 To UBound(x)
            x(i) = "(" & .Replace(x(i), "\$1") & ")"
        Next
        .Pattern = "\b(" & Join(x, "|") & ")\b"
        For Each r In Range("a1", Range("a" & Rows.Count).End(xlUp))
            For Each m In .Execute(r)
                For i = 1 To m.submatches.Count - 1
                    If m.submatches(i) <> "" Then
                        With r.Characters(m.firstindex + 1, m.Length).Font
                            .Color = mycolor(i - 1)
                            .Bold = True
                        End With
                        Exit For
                    End If
                Next
            Next
        Next
    End With
    Application.ScreenUpdating = True
End Sub
1720677557043.png
 
Upvote 0

Excel Facts

Highlight Duplicates
Home, Conditional Formatting, Highlight Cells, Duplicate records, OK to add pink formatting to any duplicates in selected range.
Why don't you just use Conditional Formatting?

If your list of words in Column I and your words to search in in Column A

Conditional Formatting formula:
=OR(ISNUMBER(FIND(" " &$I$1:$I$1000&" "," "&A1&" ")))

Book2
ABCDEFGHI
1The chairman is hereairman
2I saw the airmanpilot
3The funny papers are coolfun
Sheet1
Cells with Conditional Formatting
CellConditionCell FormatStop If True
A:AExpression=OR(ISNUMBER(FIND(" " &$I$1:$I$1000&" "," "&A1&" ")))textNO
Scott,
That does not seem to work in all cases. Consider the situations in which the phrase begins with the word you are are looking for, or there is punctuation after the word you are looking for.
Here are some examples where it does not work properly.

1720696703024.png


Rows 1 and 3 should also be formatted red.
 
Upvote 0
Scott,
That does not seem to work in all cases. Consider the situations in which the phrase begins with the word you are are looking for, or there is punctuation after the word you are looking for.
Here are some examples where it does not work properly.

View attachment 113954

Rows 1 and 3 should also be formatted red.

Depends on the source data of course, starting word could be fixed by switching to Search instead of find, but not the punctuation
 
Upvote 0
Love the regex suggestions, just have to wonder what will happen when the Scripting DLL that it relies on is deprecated.
 
Upvote 0

Forum statistics

Threads
1,221,310
Messages
6,159,176
Members
451,543
Latest member
cesymcox

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