remove after last "-" not working

dmj120

Active Member
Joined
Jan 5, 2010
Messages
286
Office Version
  1. 365
  2. 2019
  3. 2010
I'm trying to remove text after the second hyphen. Some cells have only one hyphen, while others have two. The below formula works with one hyphen

in C2 I have =LEFT(B2,FIND("@",SUBSTITUTE(B2,"-","@",LEN(B2)-LEN(SUBSTITUTE(B2,"character",""))))-1)

Example:
Col BCol C - desired result
sicu - r2sicu
burnt - d9burnt
A3 - OPP - R7A3 - OPP
QPP - Ops - dev9OPP - Ops
QPP - Ops - dev9OPP ----- what my formula outputs


Where am I making a mistake?

Thanks
Josh
 
Here is another formula that you can try...
Excel Formula:
=TRIM(LEFT(B2,IFERROR(FIND("@",SUBSTITUTE(B2,"-","@",2)),FIND("-",B2))-1))

Thanks Rick. I ended up with
=IFERROR(TRIM(LEFT(B2,FIND("@",SUBSTITUTE(B2,"-","@",LEN(B2)-LEN(SUBSTITUTE(B2,"-",""))))-1)),B2)

Why did you put iferror inside the 'left trim' ? I've never used iferror inside, only as the catch-all in front of whatever formula i'm creating.
 
Upvote 0

Excel Facts

Whats the difference between CONCAT and CONCATENATE?
The newer CONCAT function can reference a range of cells. =CONCATENATE(A1,A2,A3,A4,A5) becomes =CONCAT(A1:A5)
Why did you put iferror inside the 'left trim' ? I've never used iferror inside, only as the catch-all in front of whatever formula i'm creating.
IFERROR is a function that returns a value (either text, number, date, whatever) and, as such, can be used anywhere within a formula provided the rest of the formula is designed to make use of what it returns. In my formula, the first part of the IFERROR finds the location of the second dash... if that errors out, then it means there is only one dash and the second part of the IFERROR calculates where that dash is. So, a number is returned either way and the LEFT function uses it.
 
Upvote 0
IFERROR is a function that returns a value (either text, number, date, whatever) and, as such, can be used anywhere within a formula provided the rest of the formula is designed to make use of what it returns.

In my formula, the first part of the IFERROR finds the location of the second dash... if that errors out, then it means there is only one dash and the second part of the IFERROR calculates where that dash is. So, a number is returned either way and the LEFT function uses it.

Thanks for the explanation!
I finally feel like I'm crossing into an intermediate level..... I have an idea of how much I don't know. ?

.........getting a semi-decent 17K row file to standardize, which is then followed by a 29.5K row file (in a different format, with new and duplicate items), not to mention after file #1 was 72% complete......well, it's gotta be scotch-frickin-thirty! ? ??
 
Upvote 0
You could also do it without the introduction of a new character

Excel Formula:
=TRIM(LEFT(B2,IFERROR(FIND("-",B2,FIND("-",B2)+1),FIND("-",B2))-1))
 
Upvote 0
Also without the TRIM, and threw in the IFERROR for no hyphen:

Book3.xlsx
BC
2sicu - r2sicu
3burnt - d9burnt
4A3 - OPP - R7A3 - OPP
5QPP - Ops - dev9QPP - Ops
6QPP - Ops - dev9QPP - Ops
7abcabc
8abc - def - gh - ij -klmabc - def
Sheet937
Cell Formulas
RangeFormula
C2:C8C2=IFERROR(LEFT(B2,IFERROR(FIND("-",B2,FIND("-",B2)+1)-1,FIND("-",B2)-1)-1),B2)
 
Upvote 0
Also without the TRIM, and threw in the IFERROR for no hyphen:
]
I would leave the TRIM from Peter's reworking of my formula in just in case the space before the dash was not always there. As for handling the "no dash" error, I would just give that last FIND function call a dash to find for those cases. So, modifying what Peter posted, this will return what the OP wanted and return the same values your formula returned for those that do not have a dash...
Excel Formula:
=TRIM(LEFT(B2,IFERROR(FIND("-",B2,FIND("-",B2)+1),FIND("-",B2&"-"))-1))
I think given the combined contributions from the three of us, this formula should be the final form for the OP to use.
 
Upvote 0
Solution
That is awesome! I'm going to play with this so I can "read" it and absorb it. eg. I "read" the if() as "if this, than that, otherwise......etc.
Just noting I posted something regarding Josh's formula in Message #17 that you may want to read.
 
Upvote 0
Just noting I posted something regarding Josh's formula in Message #17 that you may want to read.

:unsure: Hey Rick, how'd you come up with Josh ?, unless you're referring to someone else, but you're close, it's Joseph. :cool:

And Yes, your latest formula post would be the shortest so far (y)
 
Upvote 0

Forum statistics

Threads
1,214,606
Messages
6,120,497
Members
448,967
Latest member
visheshkotha

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