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

Can you sort left to right?
To sort left-to-right, use the Sort dialog box. Click Options. Choose "Sort left to right"
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,223,893
Messages
6,175,240
Members
452,621
Latest member
Laura_PinksBTHFT

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