Find an exact match in a string and substitute with another value.

MrOllyR

New Member
Joined
Jun 24, 2020
Messages
24
Office Version
  1. 365
Platform
  1. Windows
I was wondering if anyone can help me.

Sentence in cell A1: The security broke up a fight that had started between the two groups of people. Ended broke_up

Extracting the main sentence without the last two words
=LEFT(TRIM(A1),FIND(CHAR(1),SUBSTITUTE(TRIM(A1)," ",CHAR(1),LEN(TRIM(A1))-LEN(SUBSTITUTE(TRIM(A1)," ",""))-1)))
Returns in A2: The security broke up a fight that had started between the two groups of people.

Extracting the last word from the string

Extracting the last word from the string "broke_up" (A1) in A3: =TRIM(RIGHT(SUBSTITUTE(A1," ",REPT(" ",100)),100))
Replacing the "_" with " " in the last word "broke_up" (A3) in order to make up to three separate words in A4: =SUBSTITUTE(A3,"_"," "). In this instance there are only two words- "broke" and "up".
Extracting each word from A4 into cells A5, A6 and A7:
A5: =TRIM(MID(SUBSTITUTE(A4," ",REPT(" ",LEN(A4))), (1-1)*LEN(A4)+1, LEN(A4))). Returns "broke"
A6: =TRIM(MID(SUBSTITUTE(A4," ",REPT(" ",LEN(A4))), (2-1)*LEN(A4)+1, LEN(A4))). Returns "up".
A7: =TRIM(MID(SUBSTITUTE(A4," ",REPT(" ",LEN(A4))), (3-1)*LEN(A4)+1, LEN(A4))). Returns nothing as there are only 2 words.

Extracting the second to last word from the string
Extracting the second to last word "Ended" from the string (A1) in A8: =TRIM(LEFT(RIGHT(" "&SUBSTITUTE(TRIM(A1)," ",REPT(" ",60)),120),60))
Replacing the "_" with " " (A8) in A9: =SUBSTITUTE(A8,"_"," "). In this instance, the string is made up of one word so will return the same value "Ended".

Output
=SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(A2,A4,"________"),A5,"________"),A6,"________")&CHAR(10)&"(" & A8 & ")"

This returns:

The security ________ ________ a fight that had started between the two gro________s of people.
(Ended)


This is good except it has substituted the letters "u" and "p" in the word "groups", too. Is there a way of adapting what I have done so it only returns words which meet the criteria and not if the letters are within other words like the example above?

Thank you in advance.
 

Excel Facts

What did Pito Salas invent?
Pito Salas, working for Lotus, popularized what would become to be pivot tables. It was released as Lotus Improv in 1989.
Sorry, it should be: =SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(A2,A5,"________"),A6,"________"),A7,"________")&CHAR(10)&"(" & A9 & ")"
 
Upvote 0
Try
Excel Formula:
=SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(A2," "&A5&" "," ________ ")," "&A6&" "," ________ ")," "&A7&" "," ________ ")&CHAR(10)&"(" & A9 & ")"
 
Upvote 0
Solution

Forum statistics

Threads
1,223,228
Messages
6,170,876
Members
452,363
Latest member
merico17

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