Excel formulae to extract text string from a cell

simongilder

Board Regular
Joined
Nov 3, 2011
Messages
68
Hi,

I am having problems with an Excel formula, I was wondering if anyone can help?
In column A, I have cells with a mix of numbers and text. The string is built according to:

Company Name + B/S+ Number + Customer Name + Reference

Rus-Hydro B 120 Smith 123456789


All elements can vary in length. I want to extract the Customer Name(eg Smith). I considered the Find and Mid function combination, but because everything varies, I can't get it to work.

Can anybody help?

Many thanks

Simon
 

Excel Facts

Select a hidden cell
Somehide hide payroll data in column G? Press F5. Type G1. Enter. Look in formula bar while you arrow down through G.
if there are no spaces in your company names then this could work

=TRIM(MID(A1;FIND(CHAR(7);SUBSTITUTE(A1;" ";CHAR(7);3));FIND(CHAR(7);SUBSTITUTE(A1;" ";CHAR(7);4))-FIND(CHAR(7);SUBSTITUTE(A1;" ";CHAR(7);3))))
 
Upvote 0
As the uncertainty goes up, so does the ugliness... but here's my attempt. Obviously, replace B3 with the relevant cell for you:

=MID(B3,FIND(" ",B3,IF(ISNUMBER(FIND(" S ",B3)),FIND(" S ",B3),FIND(" B ",B3))+3)+1,(FIND(" ",B3,FIND(" ",B3,IF(ISNUMBER(FIND(" S ",B3)),FIND(" S ",B3),FIND(" B ",B3))+3)+1)-(FIND(" ",B3,IF(ISNUMBER(FIND(" S ",B3)),FIND(" S ",B3),FIND(" B ",B3))+3)))-1)

The idea here is to set an anchor point based on " S " or " B ", which is always followed by a number (with no spaces), which is always followed by a space, then the name (with no spaces), then another space. If those assumptions hold true, this should work. It avoids the problem of a company name having spaces, but does not account for spaces that may appear in the number field or the name.

I guess a more sophisticated approach would be to first figure out how many spaces there are and use the space after the number and the last space as the binding criteria - that would account for spaces in the name...
 
Upvote 0
Hi Jackhandey

That's just the ticket. Only, I have discovered another slight complication. Some of the values in the Company name can end with an "S". In this case, it retrns the Number. In other cases, it works fine.

Here is an example of the offending combination:

Kazmun Reg S B 15713 Sampo Bank 037287

Returns 15713 instead of Sampo

Many thanks

Simon

<colgroup><col width="313"></colgroup><tbody>
</tbody>
 
Upvote 0
Ooooh, that's nasty lol. I'll think some more on it, but I imagine one of the elite users will have a much more elegant solution.

In that last example, would you want it to return "Sampo Bank" or just Sampo?
 
Last edited:
Upvote 0
Alright, this has to be one of the ugliest things I've ever written, but I think it works... it does for the two example above, anyway, and it returns "Sampo Bank" instead of just "Sampo"...

=MID(B3,FIND("$ ",SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(B3,0,"$"),1,"$"),2,"$"),3,"$"),4,"$"),5,"$"),6,"$"),7,"$"),8,"$"),9,"$"))+2,(FIND(" $",SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(B3,0,"$"),1,"$"),2,"$"),3,"$"),4,"$"),5,"$"),6,"$"),7,"$"),8,"$"),9,"$"),FIND("$ ",SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(B3,0,"$"),1,"$"),2,"$"),3,"$"),4,"$"),5,"$"),6,"$"),7,"$"),8,"$"),9,"$"))))-FIND("$ ",SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(B3,0,"$"),1,"$"),2,"$"),3,"$"),4,"$"),5,"$"),6,"$"),7,"$"),8,"$"),9,"$"))-2)

It replaces all numbers with a dollar sign and then uses the "$ " and " $" strings as the boundaries for extraction. However, it just occurred to me that if a company name ends in a number, this will blow up... grrr.

Edit - the potential fix for that would be to search for "$$$ " and " $$$" instead... what's the minimum number of numbers that can appear for each of the sets of numbers?
 
Last edited:
Upvote 0
Hi Jackhandey

Thanks so much for that. But unfortunately I get an error - there are too many nesting levels for my version of Excel. Strange, as I saved it as an Excel 2013 file.

Perhaps it would be easier just to go for the "Sampo" result. That would be fine for me.

Thanks again

Simon
 
Upvote 0

Forum statistics

Threads
1,214,390
Messages
6,119,235
Members
448,879
Latest member
VanGirl

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