Remove contents from a cell based on certain conditions

mlarson

Well-known Member
Joined
Aug 25, 2011
Messages
509
Office Version
  1. 2010
Platform
  1. Windows
Hello! I am hoping somebody can help me with this problem. Thank you in advance for your help!

I am looking for a formula (prefer not to use VBA) that will accomplish the following, with the formula/results in column B.

When the first 5 characters of a cell are repeated within that same cell, I would like for all text/characters that are after that second occurrence of those first 5 characters, including those 5 characters itself, to be cleared/removed. If there is no second occurrence of those first 5 characters, then nothing needs to be cleared/removed. Here are some examples...

Current cell (A1):(pizza 345) bananas are yellow (pizza 345) grass is green sky is blue
Desired result (B1):(pizza 345) bananas are yellow
Note:"(pizz" are the first 5 characters, so everything after second occurrence of "(pizz", including itself, is removed.
Current cell (A2):(1 pizza 345) bananas are yellow (1 pizza 345) grass is green sky is blue
Desired result (B2):(1 pizza 345) bananas are yellow
Note:"(1 pi" are the first 5 characters, so everything after second occurrence of "(1 pi", including itself, is removed.
Current cell (A3):(pizza 345) bananas (p) are yellow (pizza 345) grass is green sky is blue
Desired result (B3):(pizza 345) bananas (p) are yellow
Note:"(pizz" are the first 5 characters, so everything after second occurrence of "(pizz", including itself, is removed.
Current cell (A4):(pizza 345) bananas (pizz are yellow (pizza 345) grass is green (p) sky is blue
Desired result (B4):(pizza 345) bananas
Note:"(pizz" are the first 5 characters, so everything after second occurrence of "(pizz", including itself, is removed.
Current cell (A5):(pizza 345) bananas are yellow grass is green (p) sky is blue
Desired result (B5):(pizza 345) bananas are yellow grass is green (p) sky is blue
Note:"(pizz" not repeated in the cell, so nothing cleared/removed.


Hopefully I've explained that clearly enough. Thank you for your help!
-Mark
 

Excel Facts

What do {} around a formula in the formula bar mean?
{Formula} means the formula was entered using Ctrl+Shift+Enter signifying an old-style array formula.
@mlarson Mark, try this.
Book1
AB
1(pizza 345) bananas are yellow (pizza 345) grass is green sky is blue(pizza 345) bananas are yellow
2(1 pizza 345) bananas are yellow (1 pizza 345) grass is green sky is blue(1 pizza 345) bananas are yellow
3(pizza 345) bananas (p) are yellow (pizza 345) grass is green sky is blue(pizza 345) bananas (p) are yellow
4(pizza 345) bananas (pizz are yellow (pizza 345) grass is green (p) sky is blue(pizza 345) bananas
5(pizza 345) bananas are yellow grass is green (p) sky is blue(pizza 345) bananas are yellow grass is green (p) sky is blue
Sheet1
Cell Formulas
RangeFormula
B1:B5B1=TRIM(LEFT(SUBSTITUTE(A1,LEFT(A1,5),REPT(" ",999),2),999))

Hope that helps.
 
Upvote 0
This would also be an option I believe.

22 03 06.xlsm
AB
1(pizza 345) bananas are yellow (pizza 345) grass is green sky is blue(pizza 345) bananas are yellow
2(1 pizza 345) bananas are yellow (1 pizza 345) grass is green sky is blue(1 pizza 345) bananas are yellow
3(pizza 345) bananas (p) are yellow (pizza 345) grass is green sky is blue(pizza 345) bananas (p) are yellow
4(pizza 345) bananas (pizz are yellow (pizza 345) grass is green (p) sky is blue(pizza 345) bananas
5(pizza 345) bananas are yellow grass is green (p) sky is blue(pizza 345) bananas are yellow grass is green (p) sky is blue
Clear text
Cell Formulas
RangeFormula
B1:B5B1=TRIM(LEFT(A1,FIND(LEFT(A1,5),A1&A1,2)-1))



Just to clarify:
To strictly do what you have asked, my formula should not have the TRIM function around it as, in the first example, this removes the space character after the word "yellow" and that space character is not part of the first 5 characters, nor after that.
I have assumed that you would want that space removed though so that the final text ends with "yellow" not "yellow " :)

A somewhat similar issue occurs with @Snakehips' formula, though if TRIM were removed from that formula, there would be many space characters at the end of the text.
 
Upvote 0

Forum statistics

Threads
1,214,974
Messages
6,122,536
Members
449,088
Latest member
RandomExceller01

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