Remove variable length number from string

Philip041

Board Regular
Joined
Jul 17, 2011
Messages
63
Hello all,

Looking for some help trying to clean some data I have. I have a list of names which have been given a number suffix but unfortunately not in the same way.

My attempt so far: in cell A1 I have "Johnathan Green - 159230". In cell B1 I put "=LEFT(B2, LEN(B2)-9)" to give me just the name, removing the number and also the " - " which was sandwiched between the name and the number.

Complication though:
  • The numbers are not always the same length, most often they are 5 or 6 digits long but I have found others which are 3 or 4.
  • The " - " sandwich between the name and the number is sometimes a ":"
  • Sometimes there are numbers (or hyphens or colons) in the name eg. "Johnathan1 Green - 159230", or "Johnathan - Green - 159230" etc. However as a rule the part I want to get rid of always starts with a " - " or ":" (it just might not be the first time that a " - " ":" appears in the string).

I hope that makes sense. Any help much appreciated!

Philip
 
Thanks Mike, sorry I didn't make clear that sometimes it could be separate by just a colon eg. "Johnathan Green:159230"..
If you want the result as a number then try this

=RIGHT(SUBSTITUTE(SUBSTITUTE(A2,":"," ")," ",REPT(" ",30)),30)+0

However, if the text might be like Johnathan - Green:000159 and you want the text result 000159 then try

=TRIM(RIGHT(SUBSTITUTE(SUBSTITUTE(A2,":"," ")," ",REPT(" ",30)),30))
 
Upvote 0

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.
I managed to get there in the end. This seems to work ok, it's just previous answers rearranged.

=TRIM(RIGHT(D2,LEN(D2)-FIND("-",SUBSTITUTE(D2,":","-"),LEN(D2)-8)))

Thanks a lot for the help.
If that logic works, then these might too?
Again depends on whether you want the result as a number or as text.

=REPLACE(D2,1,MIN(FIND({"-",":"},D2&"-:",LEN(D2)-9)),"")+0

=TRIM(REPLACE(D2,1,MIN(FIND({"-",":"},D2&"-:",LEN(D2)-9)),""))
 
Upvote 0
Please start a new thread for your question & post some sample data. Excel has changed a lot since 2017
 
Upvote 0

Forum statistics

Threads
1,215,429
Messages
6,124,842
Members
449,193
Latest member
MikeVol

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