Formula to remove all but the last of the same character in a cell

James01

Board Regular
Joined
Oct 29, 2008
Messages
124
Hi

I have data in cell A1 and A2 which looks like below

SECTOR - 11, HIRAN MAGRI, - 313001
MAIN BRANCH, 30-15-171 DABA GARDENS, NEAR SARASWATI PARK,-530020,

In cell A1 the number of hypens (-) are 2
In cell A2 the number of - are 3

What I am looking for is a formula which can remove all additional - except the last one. Therefore the result of the formula should be

SECTOR 11, HIRAN MAGRI, - 313001
MAIN BRANCH, 30 15 171 DABA GARDENS, NEAR SARASWATI PARK,-530020

Is this possible by using a formula

Thanks for the replies
 

Excel Facts

What is the fastest way to copy a formula?
If A2:A50000 contain data. Enter a formula in B2. Select B2. Double-click the Fill Handle and Excel will shoot the formula down to B50000.
I have not seen this ability using a formula, but you can do this via a macro.

Hmmm, am I a formula God like Domenic, then? ;-)

=SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(A1,"-","§",LEN(A1)-LEN(SUBSTITUTE(A1,"-",""))),"-"," "),"§","-")

Wigi
 
Upvote 0
wigi,

Can you explain this formula a little bit, as I am a little lost in it actions?

Sure.

=LEN(A1)-LEN(SUBSTITUTE(A1,"-",""))

This calculates the number of - in A1. Say, this is 3.

=SUBSTITUTE(A1,"-","§",3)

This takes A1 and substitutes (ONLY) the 3rd - into a §, a character that does not occur in the cells (assumption).

The last but one step, is in that resulting string, replace - with "" (so deleting -) We don't delete the 3rd - because there are only 2 - in the string (and 1 §)

Last step is to replace § with -

Hope this is now clear? If not, ask again please.
 
Upvote 0
Hmmm, am I a formula God like Domenic, then? ;-)

I will have to give it to you. Even on a Friday afternoon, there is still something to be learned.

Great job and great explanation.

It appears that the replacement is replacing with a space though, maybe it is just the eyes going on a Friday afternoon.
=SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(A1,"-","§",LEN(A1)-LEN(SUBSTITUTE(A1,"-",""))),"-"," "),"§","-")
 
Upvote 0
It was indeed a space, thans for catching that one.
 
Upvote 0
James, in your examples the strings end with a 6 digit number preceeded by a hyphen. Is it always like that?
 
Upvote 0

Forum statistics

Threads
1,224,808
Messages
6,181,073
Members
453,020
Latest member
mattg2448

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