Replace in VBA acting odd

Oliver Dewar

Board Regular
Joined
Apr 17, 2011
Messages
201
Hi All,

I have a macro that replaces a token in a string of text with the content of a cell. 99% of the time this works perfectly and the tokens are replaced.

Occasionally, excel decides it's going to replace every occurance of the token in the whole sheet! I've run this hundreds of times and it only happens once in every 300... odd.

Here's my code:


Code:
Sheets("Admin").Range("S10").Replace "*TOKEN*
", Sheets("Admin").Range("O20").Value
</P>
Is there any way to absolutely restrict the replace to that cell (S10)?

Or any suggestions as to what's going on?
 

Excel Facts

How to create a cell-sized chart?
Tiny charts, called Sparklines, were added to Excel 2010. Look for Sparklines on the Insert tab.
Couldn't you just use something like this?
Code:
If Sheets("Admin").Range("S10") Like "*TOKEN* Then
    Sheets("Admin").Range("S10").Value = Sheets("Admin").Range("O20").Value
End If
 
Upvote 0
I haven't come across the 'Like' before... does that find part of a string within a string?

Also, the token is a word, being replaced by another word while the rest of the string stays the same, so replacing the whole of S10 with range O20 would delete all the rest of the string.
 
Upvote 0

Forum statistics

Threads
1,224,605
Messages
6,179,860
Members
452,948
Latest member
UsmanAli786

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