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

VBA learner ITG

Board Regular
Joined
Apr 18, 2017
Messages
208
Office Version
  1. 365
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
 

Some videos you may like

Excel Facts

Can you sort left to right?
To sort left-to-right, use the Sort dialog box. Click Options. Choose "Sort left to right"

VBA learner ITG

Board Regular
Joined
Apr 18, 2017
Messages
208
Office Version
  1. 365
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
 

Watch MrExcel Video

Forum statistics

Threads
1,127,959
Messages
5,627,861
Members
416,278
Latest member
CellHell

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
Top