Find and replace macro targeting words within words - can it target only whole words within a string?

dougmarkham

Active Member
Joined
Jul 19, 2016
Messages
252
Office Version
  1. 365
Platform
  1. Windows
Hi Folks,

I am attempting to use VBA to modify address data because the first two address columns have character limits and my data tends to exceed the limits.
I wish to modify text strings within cells i.e., to find and replace words within text strings with abbreviations. For the below code, I have compiled a fndList and rplcList (table 2) to achieve this.

I am using this code:

Code:
Sub Multi_FindReplace()

Dim sht As Worksheet
Dim fndList As Integer
Dim rplcList As Integer
Dim tbl As ListObject
Dim myArray As Variant


'Create variable to point to your table
  Set tbl = Worksheets("Abbrev").ListObjects("Table2")


'Create an Array out of the Table's Data
  Set TempArray = tbl.DataBodyRange
  myArray = Application.Transpose(TempArray)
  
'Designate Columns for Find/Replace data
  fndList = 1
  rplcList = 2


'Loop through each item in Array lists
  For x = LBound(myArray, 1) To UBound(myArray, 2)
    'Loop through each worksheet in ActiveWorkbook (skip sheet with table in it)
          Selection.Replace what:=myArray(fndList, x), replacement:=myArray(rplcList, x), _
            [COLOR=#008000][B]LookAt:=xlPart[/B][/COLOR], SearchOrder:=xlByRows, MatchCase:=False, _
            SearchFormat:=False, ReplaceFormat:=False
  Next x


End Sub

As an example of the problem.

I'm trying to replace words such as:
and--with-->&
field--with-->fld

The below code works but is altering place names like:
Sunderland--to-->Sunderl&
Sheffield---to-->Sheffld

I have to stipulate a partial match because my cell contents are e.g., Sunderland and Shields Building Society
Code:
LookAt:=xlPart, SearchOrder:=xlByRows, MatchCase:=False, _
            SearchFormat:=False, ReplaceFormat:=False
In this example, I would like: and--to be-->&; Building--to be-->Bldg and Society--to be-->Soc (Sunderland & Shields Bldg Soc); therefore, I need a partial match.

At the moment, as it's finding partial matches 'inside words' in a string, I wonder if there is a way to modify the code to allow partial match but only against whole words within a string i.e., so Sunderland would remain Sunderland but "and" would become "&"?

If it is possible to achieve this, would anybody be willing to help me modify this code please?

Kind regards,

Doug
 

Excel Facts

Copy formula down without changing references
If you have =SUM(F2:F49) in F50; type Alt+' in F51 to copy =SUM(F2:F49) to F51, leaving the formula in edit mode. Change SUM to COUNT.
Doug,

Have you thought of including spaces into your fndList? This way, when searching for " and " instead of "and", only the single word "and" would be replaced. Of course you'd have to include spaces in the rplcList as well.
 
Upvote 0
Doug,

Have you thought of including spaces into your fndList? This way, when searching for " and " instead of "and", only the single word "and" would be replaced. Of course you'd have to include spaces in the rplcList as well.

Hi Tim,

That is exactly what I hadn't thought of---works a treat, and so simple!
Thanks very much for you help!

Kind regards,

Doug.
 
Upvote 0

Forum statistics

Threads
1,223,903
Messages
6,175,286
Members
452,631
Latest member
a_potato

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