add text before and after first occurrence string

Okoth

Board Regular
Joined
Sep 10, 2009
Messages
106
Office Version
  1. 2019
Platform
  1. Windows
I want to find a string in one cell and add text before and after the first occurrence of that string. An example is easier to explain. I want to add <strong> before time and </strong> after time. What is important is that it only happens with the first occurrence of the word time. A1 is the original text and B1 is the result of the formula.

A​
B​
1​
This is the first time the string occurs and this is the second time it shows up.This is the first<strong>time</strong> the string occurs and this is the second time it shows up.

To get <strong> before the first "time" wasn't hard. I managed with:

Excel Formula:
=MID(A1;1;FIND("time";A1)-1)&"<strong>"&MID(A1;FIND("time";A1);9999)

but how do I get </strong> after the first "time" in A1? I want to do this with a formula not VBA.
 
Last edited:

Excel Facts

Show numbers in thousands?
Use a custom number format of #,##0,K. Each comma after the final 0 will divide the displayed number by another thousand
I suggest that you update your Account details (or click your user name at the top right of the forum) so helpers always know what Excel version(s) & platform(s) you are using as the best solution often varies by version. (Don’t forget to scroll down & ‘Save’)

Try this

21 12 12.xlsm
AB
1This is the first time the string occurs and this is the second time it shows up.This is the first <strong>time</strong> the string occurs and this is the second time it shows up.
Sheet2 (2)
Cell Formulas
RangeFormula
B1B1=SUBSTITUTE(A1,"time","<strong>time</strong>",1)
 
Upvote 0
Solution
try

=SUBSTITUTE(A1;"time";"<strong>"&"time"&"<strong>";1)
 
Upvote 0
I suggest that you update your Account details (or click your user name at the top right of the forum) so helpers always know what Excel version(s) & platform(s) you are using as the best solution often varies by version. (Don’t forget to scroll down & ‘Save’)
Thanks, I will. I haven't been here for a long time. The site has changed for the better.

Thank you both for the answer.
 
Upvote 0
You're welcome. Thanks for the follow-up. :)
 
Upvote 0

Forum statistics

Threads
1,220,965
Messages
6,157,119
Members
451,398
Latest member
rjsteward

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