VBA Advice on find and replace whole word within a text string

VBA learner ITG

Active Member
Joined
Apr 18, 2017
Messages
272
Office Version
  1. 365
Platform
  1. Windows
  2. MacOS
Hi all,

I need your guidance if possible.

The below code currently works by finding a part word from a table and amending part word to a what the user wants.

However when i amend the line of code to look at the whole word within a text string it doesnt perform the amendments.

Current Line of Code i amended: LookAt:=xlpart
New Line of Code which I have tried:to LookAt:=xlwhole

For example:

If i wanted to replace the word bottle to Bottle with a (capital B) so it looks like the below:

Current text string:Diet Coke 4 Pack Glass bottle
Required output: Diet Coke 4 Pack Glass Bottle



VBA Code:
Dim wb As Workbook
Dim ws As Worksheet
Dim ws_client As Worksheet
Dim tbl As ListObject
Dim lrow As Range

Set wb = ActiveWorkbook
Set ws = wb.Sheets("MAIN BRIEF")
Set ws_client = wb.Sheets("CLIENT_FACING")

Set tbl = ws_client.ListObjects("Table1")
    For Each lrow In tbl.ListColumns(1).DataBodyRange.Rows
    find_str = lrow.Offset(0, 0)
    rep_str = lrow.Offset(0, 1)
    ws.Cells.Replace what:=find_str, Replacement:=rep_str, _
    LookAt:=xlpart, SearchOrder:=xlByRows, MatchCase:=False, _
    SearchFormat:=False, ReplaceFormat:=False
Next lrow
Set tbl = Nothing
Set ws = Nothing
Set ws_client = Nothing
Set wb = Nothing
End Sub


Below is a table of words the above code is performing the find and replace:

WORD TO FINDWORD TO REPLACE WITH IN THE SENTANCE
kitkatKitKat
Dark MilkDarkmilk
Ltrsltr
BbqBBQ
Co-OpCo-op
ChocoalteChocolate
Rowntree'SRowntree's
PastilesPastilles
XlXL
MullerMüller
NestleNestlé
Gu
McVitiesMcVitie’s
CrimblesMrs Crimbles
Chefs CuisineChef’s Cuisine
RowntreesRowntree’s
MccainMcCain
Haagen DazsHaagen-Dazs
WallsWall’s
YoungsYoung’s
Ben & JerrysBen & Jerry’s
Harriets GardenHarriet’s Garden
CheesestringsCheestrings
Emmi Caffe LatteEmmi Caffé Latte
Rachels OrganicRachel’s Organic
JacksonsJackson’s
sfcSFC
GoodfellasGoodfella’s
MoophoriaMoo-Phoria
JudesJude’s
KellysKelly’s
KitkatKitKat
N/ValleyNature Valley
NASNo Added Sugar
4PK4 Pack
RegRegular
S/SationsSensations
G/WonderGolden Wonder
Kit KatKitKat
T & LyleTate & Lyle
J WestJohn West
GreengntGreen Giant
L/GManLoyd Grossman
WULWash Up Liquid
APAAnti Perspirant Deodorant
APDAnti Perspirant Deodorant
F/BerioFilippo Berio
O.E. PasoOld El Paso
U/BensUncle Bens
F/BentosFray Bentos
H/HoopsHula Hoops
CandylndCandyland
F/ShootFruit Shoot
LSLow Sugar
RobsRobinsons
S/CapSports Cap
RobsRobinsons
S/CapSports Cap
 

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.
Per the above I believe I have fixed the issue:

VBA Code:
Dim wb As Workbook
Dim ws As Worksheet
Dim ws_client As Worksheet
Dim tbl As ListObject
Dim lrow As Range

Set wb = ActiveWorkbook
Set ws = wb.Sheets("MAIN BRIEF")
Set ws_client = wb.Sheets("CLIENT_FACING")

Set tbl = ws_client.ListObjects("Table1")
    For Each lrow In tbl.ListColumns(1).DataBodyRange.Rows
    find_str = lrow.Offset(0, 0)
    rep_str = lrow.Offset(0, 1)
    ws.Cells.Replace what:=find_str, Replacement:=rep_str, _
    LookAt:=xlPart, SearchOrder:=xlByRows, MatchCase:=True, _
    SearchFormat:=False, ReplaceFormat:=False
Next lrow
Set tbl = Nothing
Set ws = Nothing
Set ws_client = Nothing
Set wb = Nothing
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,828
Messages
6,181,209
Members
453,022
Latest member
RobertV1609

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