Replace Words with exact match in Large Text using VBA

mathsbeauty

Board Regular
Joined
Apr 23, 2011
Messages
89
The large text is imported from clipboard. I want to have a replace function in excel vba similar to replace function in ms word with the option find "whole words only". The replace function in VBA or worksheet function substitute is of no use. Here is what I am trying to do:
Code:
Dim DataObj As MSForms.DataObject
Set DataObj = New MSForms.DataObject
DataObj.GetFromClipboard
Text = DataObj.GetText(1)
NewText = replace(Text, "101", "102")


This replaces 101 to 102 but also 1010 to 1020. I just want to replace with exact match (that is just want to replace 101 by 102 but not 1010 to 1020). How can I have excel vba function to achieve this?
 
Last edited:

Excel Facts

Return population for a City
If you have a list of cities in A2:A100, use Data, Geography. Then =A2.Population and copy down.
Assuming the only delimiter is space,

Code:
  Dim oDO           As MSForms.DataObject
  Dim s             As String

  Set oDO = New MSForms.DataObject
  oDO.SetText "101 1010 102 101"
  oDO.PutInClipboard

  oDO.GetFromClipboard
  s = oDO.GetText(1)
  s = Trim(Replace(" " & s & " ", " 101 ", " 102 "))
  Debug.Print s
 
Last edited:
Upvote 0

Forum statistics

Threads
1,224,820
Messages
6,181,160
Members
453,021
Latest member
Justyna P

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