Substitute string in cell if one of many

Okoth

Board Regular
Joined
Sep 10, 2009
Messages
106
Office Version
  1. 2019
Platform
  1. Windows
I am totally stuck with this. I have tried helper cells, substitute and sumif formulas but I cannot get what I want.

So, I have a list with values. There is a text and if a word in the text matches a word in the list then it has to be replaced with a word that is in a cell next to the cell in the list.

An example

[TABLE="class: grid, width: 600, align: left"]
<tbody>[TR]
[TD]TEXT[/TD]
[TD]LIST[/TD]
[TD]becomes[/TD]
[TD]THIS[/TD]
[/TR]
[TR]
[TD]The white cat will be replaced by a black fish[/TD]
[TD]duck[/TD]
[TD]becomes[/TD]
[TD]mouse[/TD]
[/TR]
[TR]
[TD][/TD]
[TD]cat[/TD]
[TD]becomes[/TD]
[TD]fish[/TD]
[/TR]
[TR]
[TD][/TD]
[TD]dog[/TD]
[TD]becomes[/TD]
[TD]bird[/TD]
[/TR]
[TR]
[TD][/TD]
[TD]sheep[/TD]
[TD]becomes[/TD]
[TD]rat[/TD]
[/TR]
[TR]
[TD][/TD]
[TD]monkey[/TD]
[TD]becomes[/TD]
[TD]owl[/TD]
[/TR]
[TR]
[TD]Result:[/TD]
[TD]worm[/TD]
[TD]becomes[/TD]
[TD]eagle[/TD]
[/TR]
[TR]
[TD]The white fish will be replaced by a black fish[/TD]
[TD]goose[/TD]
[TD]becomes[/TD]
[TD]mole[/TD]
[/TR]
</tbody>[/TABLE]

How can I do this?
 
Last edited:

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.
Hi,

Try this :

A7 =SUBSTITUTE(A1,INDEX(B1:B7,SUMPRODUCT(--ISNUMBER(SEARCH(CONCATENATE("*",B1:B7,"*"),CONCATENATE("*",SUBSTITUTE(A1," ","*"),"*")))*ROW(B1:B7))),INDEX(D1:D7,SUMPRODUCT(--ISNUMBER(SEARCH(CONCATENATE("*",B1:B7,"*"),CONCATENATE("*",SUBSTITUTE(A1," ","*"),"*")))*ROW(B1:B7))))

[TABLE="width: 580"]
<colgroup><col><col><col span="3"></colgroup><tbody>[TR]
[TD][/TD]
[TD]A[/TD]
[TD]B[/TD]
[TD]C[/TD]
[TD]D[/TD]
[/TR]
[TR]
[TD]1[/TD]
[TD]The white cat will be replaced by a black fish[/TD]
[TD]duck[/TD]
[TD]becomes[/TD]
[TD]mouse[/TD]
[/TR]
[TR]
[TD]2[/TD]
[TD][/TD]
[TD]cat[/TD]
[TD]becomes[/TD]
[TD]fish[/TD]
[/TR]
[TR]
[TD]3[/TD]
[TD][/TD]
[TD]dog[/TD]
[TD]becomes[/TD]
[TD]bird[/TD]
[/TR]
[TR]
[TD]4[/TD]
[TD][/TD]
[TD]sheep[/TD]
[TD]becomes[/TD]
[TD]rat[/TD]
[/TR]
[TR]
[TD]5[/TD]
[TD][/TD]
[TD]monkey[/TD]
[TD]becomes[/TD]
[TD]owl[/TD]
[/TR]
[TR]
[TD]6[/TD]
[TD]Result:[/TD]
[TD]worm[/TD]
[TD]becomes[/TD]
[TD]eagle[/TD]
[/TR]
[TR]
[TD]7[/TD]
[TD]The white fish will be replaced by a black fish[/TD]
[TD]goose[/TD]
[TD]becomes[/TD]
[TD]mole[/TD]
[/TR]
</tbody>[/TABLE]
 
Upvote 0
Thank you. The example worked. Going to try and get it working in my work sheet.
 
Upvote 0
I found out that it the solution doesn't work when the list cells contain more than one word. Is there a way to adjust the solution above?

Sorry for the double work.
 
Upvote 0
If there are more than 2 words I would suggest to considering a VBA solution...

Ctrl+Shift+Enter NOT just Enter

A7 =SUBSTITUTE(SUBSTITUTE(A1,INDEX(B1:B7,SMALL(LARGE(--ISNUMBER(SEARCH(CONCATENATE("*",B1:B7,"*"),CONCATENATE("*",SUBSTITUTE($A$1," ","*"),"*")))*ROW(B1:B7),{1,2}),{1,2})),INDEX(D1:D7,SMALL(LARGE(--ISNUMBER(SEARCH(CONCATENATE("*",B1:B7,"*"),CONCATENATE("*",SUBSTITUTE($A$1," ","*"),"*")))*ROW(B1:B7),{1,2}),{1,2}))),INDEX(B1:B7,LARGE(--ISNUMBER(SEARCH(CONCATENATE("*",B1:B7,"*"),CONCATENATE("*",SUBSTITUTE($A$1," ","*"),"*")))*ROW(B1:B7),{1,2})),INDEX(D1:D7,LARGE(--ISNUMBER(SEARCH(CONCATENATE("*",B1:B7,"*"),CONCATENATE("*",SUBSTITUTE($A$1," ","*"),"*")))*ROW(B1:B7),{1,2})))


[TABLE="width: 799"]
<colgroup><col><col><col span="3"></colgroup><tbody>[TR]
[TD][/TD]
[TD]A[/TD]
[TD]B[/TD]
[TD]C[/TD]
[TD]D[/TD]
[/TR]
[TR]
[TD]1[/TD]
[TD]The white cat and the black sheep will be replaced by a black fish and white rat[/TD]
[TD]duck[/TD]
[TD]becomes[/TD]
[TD]mouse[/TD]
[/TR]
[TR]
[TD]2[/TD]
[TD][/TD]
[TD]cat[/TD]
[TD]becomes[/TD]
[TD]fish[/TD]
[/TR]
[TR]
[TD]3[/TD]
[TD][/TD]
[TD]dog[/TD]
[TD]becomes[/TD]
[TD]bird[/TD]
[/TR]
[TR]
[TD]4[/TD]
[TD][/TD]
[TD]sheep[/TD]
[TD]becomes[/TD]
[TD]rat[/TD]
[/TR]
[TR]
[TD]5[/TD]
[TD][/TD]
[TD]monkey[/TD]
[TD]becomes[/TD]
[TD]owl[/TD]
[/TR]
[TR]
[TD]6[/TD]
[TD]Result:[/TD]
[TD]worm[/TD]
[TD]becomes[/TD]
[TD]eagle[/TD]
[/TR]
[TR]
[TD]7[/TD]
[TD]The white fish and the black rat will be replaced by a black fish and white rat[/TD]
[TD]goose[/TD]
[TD]becomes[/TD]
[TD]mole[/TD]
[/TR]
</tbody>[/TABLE]
 
Upvote 0
Sorry, I think I wasn't clear enough. I meant in the B column a cell could be having more than one word.

[TABLE="width: 691"]
<tbody>[TR]
[TD][/TD]
[TD]A[/TD]
[TD]B[/TD]
[TD]C[/TD]
[TD]D[/TD]
[/TR]
[TR]
[TD]1[/TD]
[TD]The white hairy dog will be replaced by a black fish[/TD]
[TD]duck[/TD]
[TD]becomes[/TD]
[TD]mouse[/TD]
[/TR]
[TR]
[TD]2[/TD]
[TD][/TD]
[TD]cat[/TD]
[TD]becomes[/TD]
[TD]fish[/TD]
[/TR]
[TR]
[TD]3[/TD]
[TD][/TD]
[TD]hairy dog[/TD]
[TD]becomes[/TD]
[TD]bird[/TD]
[/TR]
[TR]
[TD]4[/TD]
[TD][/TD]
[TD]sheep[/TD]
[TD]becomes[/TD]
[TD]rat[/TD]
[/TR]
[TR]
[TD]5[/TD]
[TD]Result:[/TD]
[TD]monkey[/TD]
[TD]becomes[/TD]
[TD]owl[/TD]
[/TR]
[TR]
[TD]6[/TD]
[TD]The white bird will be replaced by a black fish[/TD]
[TD]worm[/TD]
[TD]becomes[/TD]
[TD]eagle[/TD]
[/TR]
[TR]
[TD]7[/TD]
[TD][/TD]
[TD]goose[/TD]
[TD]becomes[/TD]
[TD]mole[/TD]
[/TR]
</tbody>[/TABLE]
 
Last edited:
Upvote 0
:)

A7 =SUBSTITUTE(A1,INDEX(B1:B7,SUMPRODUCT(--ISNUMBER(SEARCH(SUBSTITUTE(CONCATENATE("*",B1:B7,"*")," ","*"),CONCATENATE("*",SUBSTITUTE(A1," ","*"),"*")))*ROW(B1:B7))),INDEX(D1:D7,SUMPRODUCT(--ISNUMBER(SEARCH(SUBSTITUTE(CONCATENATE("*",B1:B7,"*")," ","*"),CONCATENATE("*",SUBSTITUTE(A1," ","*"),"*")))*ROW(B1:B7))))

[TABLE="width: 619"]
<colgroup><col><col><col span="3"></colgroup><tbody>[TR]
[TD][/TD]
[TD]A[/TD]
[TD]B[/TD]
[TD]C[/TD]
[TD]D[/TD]
[/TR]
[TR]
[TD]1[/TD]
[TD]The white hairy dog will be replaced by a black fish[/TD]
[TD]duck[/TD]
[TD]becomes[/TD]
[TD]mouse[/TD]
[/TR]
[TR]
[TD]2[/TD]
[TD][/TD]
[TD]cat[/TD]
[TD]becomes[/TD]
[TD]fish[/TD]
[/TR]
[TR]
[TD]3[/TD]
[TD][/TD]
[TD]hairy dog[/TD]
[TD]becomes[/TD]
[TD]bird[/TD]
[/TR]
[TR]
[TD]4[/TD]
[TD][/TD]
[TD]sheep[/TD]
[TD]becomes[/TD]
[TD]rat[/TD]
[/TR]
[TR]
[TD]5[/TD]
[TD][/TD]
[TD]monkey[/TD]
[TD]becomes[/TD]
[TD]owl[/TD]
[/TR]
[TR]
[TD]6[/TD]
[TD]Result:[/TD]
[TD]worm[/TD]
[TD]becomes[/TD]
[TD]eagle[/TD]
[/TR]
[TR]
[TD]7[/TD]
[TD]The white bird will be replaced by a black fish[/TD]
[TD]goose[/TD]
[TD]becomes[/TD]
[TD]mole[/TD]
[/TR]
</tbody>[/TABLE]
 
Upvote 0
That's really amazing. Wish I knew as much about Excel as you. Thank you very much. I really appreciate all the time you have put into it!
 
Upvote 0
Hi,

I have a much simpler solution if you're still interested.
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,177
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