View Single Post
  #11   Report Post  
Posted to microsoft.public.excel.worksheet.functions
Rick Rothstein \(MVP - VB\) Rick Rothstein \(MVP - VB\) is offline
external usenet poster
 
Posts: 2,202
Default Extract specific text

Okay, I'm awake again... see inline comments.

=LEFT(MID(TRIM(A1),FIND(", ",TRIM(A1))+2,255),FIND(" ",MID(TRIM(A1),
FIND(", ",TRIM(A1))+2,255)))&REPLACE(LEFT(TRIM(A1),
FIND(", ",TRIM(A1))-1),1,FIND(" ",TRIM(A1),FIND(" ",TRIM(A1))+1),"")


That formula seems to still be dependent on token numbers...


Or do you mean assuming the name is the 3rd and 4th tokens combined, which
are separated by ", "?


Yes, that is what I meant. My choice of the word "numbers" was not a
particularly good one. Sorry.


. . . This formula **only** requires the last and first names to
be separated by the first comma-space pair in the string...

=MID(A1,2+FIND(", ",A1),FIND(" ",A1,2+FIND(", ",A1))-2
-FIND(", ",A1))&" "&REPLACE(SUBSTITUTE(LEFT(" "&A1,
1+FIND(", ",A1)-1)," ",CHAR(1),LEN(LEFT(A1,FIND(", ",A1)))
-LEN(SUBSTITUTE((LEFT(A1,FIND(", ",A1)))," ",""))+1),1,
FIND(CHAR(1),SUBSTITUTE(LEFT(" "&A1,1+FIND(", ",A1)-1)," ",CHAR(1),
LEN(LEFT(A1,FIND(", ",A1)))-LEN(SUBSTITUTE((LEFT(A1,FIND(", ",A1))),
" ",""))+1)),"")


If you want to use ", " as the indicator of where the name is, taking the
tokens to the left and right of it no matter where they appear in A1, and
swapping their order, use


Yes, that was what my tired mind was trying to convey.


=TRIM(MID(A1,FIND(", ",A1)+2,FIND(" ",TRIM(MID(A1,FIND(", ",A1)+2,
255)))))&MID(LEFT(A1,FIND(", ",A1)-1),FIND(", ",SUBSTITUTE(LEFT(A1,
FIND(", ",A1))," ",", ",FIND(", ",A1)-LEN(SUBSTITUTE(LEFT(A1,
FIND(", ",A1))," ","")))),255)


Although I don't think it will affect the OP's usage, the above formula, as
written, requires at least one blank space to proceed the last name... the
formula I posted doesn't. I'm thinking here of something like this...

TAYLOR, GEORGE 15:00 - 23:00 x etc.

Here is a modification to your formula which accounts for that condition...

=MID(A1,FIND(", ",A1)+2,FIND(" ",MID(A1,FIND(",
",A1)+2,255)))&MID(LEFT(A1,FIND(", ",A1)-1),FIND(", ",SUBSTITUTE(LEFT("
"&A1,1+FIND(", ",A1)-1)," ",", ",FIND(", ",A1)-LEN(SUBSTITUTE(LEFT("
"&A1,1+FIND(", ",A1)-1)," ","")))),255)

Yes, I removed the TRIM function calls, but if you test it, you will find
neither is unnecessary. Actually, the trailing space the first one was
suppressing is needed for the modification I made to work (it supplies the
space that is separating the two names). Anyway, my testing indicates that,
with the exception of comma-space delimiter between the names, you can put
as many spaces as you want elsewhere and the modified formula will not be
affected by it. It also looks like this formula follows the general
structure of my original attempt and is probably represents the code
reduction I thought could be applied to it (which is another way of saying I
won't be revisiting my originally posted code anymore<g).

Rick