Replace only whole word

capson

Board Regular
Joined
Jul 9, 2010
Messages
107
Hello,

I want to replace only whole words in a cell

Also, I do not want to use Replacement, I want something I can use with Arrays

The function does a good job of finding only the whole words but is there a way to get it replace the whole word with the MatchExprValue

Ex
cell value =HUD, HOPHUD, Justice SystemHUD, Department of Health, Department of HUD Labor, DeparHUDtment of Social, HUD


Replace HUD with ZZZ


cell value =
ZZZ, HOPHUD, Justice SystemHUD, Department of Health, Department of ZZZ Labor, DeparHUDtment of Social, ZZZ


Thanks

Code:
Sub Test_SSS()
 Dim i As Long, m As Boolean
 
    For i = 2 To LastRow    
        m = WordMatch(Range("C" & i), Range("E1"))
        MsgBox "**" & Range("C" & i) & "**  matches  **" & Range("E1") & "**  -> " & m
    Next


End Sub


Function WordMatch(Source As String, MatchExprValue As String) As Boolean
    Dim RE As Object
    Set RE = CreateObject("vbscript.regexp")
    RE.IgnoreCase = False
    RE.Pattern = "\b" & MatchExprValue & "\b"
    WordMatch = RE.test(Source)
End Function
 
Last edited:

Excel Facts

Add Bullets to Range
Select range. Press Ctrl+1. On Number tab, choose Custom. Type Alt+7 then space then @ sign (using 7 on numeric keypad)
Not entirely clear to me. Can you clarify what you have, where, and exactly what you are trying to end up with, and where?
 
Upvote 0

Forum statistics

Threads
1,214,914
Messages
6,122,211
Members
449,074
Latest member
cancansova

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