Microsoft Excel - Macro to extract text between two symbols?

Asked By Cyndy S on 10-Nov-09 08:33 PM
Hello.  I'm fairly new to Excel VBA and I've been striking out with making an excel function that pulls the text from between two symbols.  It would be something like xxx|yyy%zzz.

I've had success pulling from one side or the other but when it comes to grabbing the middle part it's a no-go.
I can get xxx
I can get zzz
I can't get yyy

I used this format for the first and second argument
Pipe$ = Left$(String$, PipePlace& - 1)

with one being left and the other being right and PipePlace being a InStr defined dim, but is there a way to grab the middle with this same kind of format?  I'm running out of ideas.
Jonathan VH replied to Cyndy S on 10-Nov-09 08:52 PM
Mid("xxx|yyy%zzz",InStr("xxx|yyy%zzz","|")+1,InStr("xxx|yyy%zzz","%")-InStr("xxx|yyy%zzz","|")-1)

Do you want this in VBA or Excel function?

[)ia6l0 iii replied to Cyndy S on 10-Nov-09 09:41 PM
=MID(A1,FIND("|",A1)+1,LEN(A1)-FIND("%", A1)-1)

If A1 had the value, and if you place the below formula in A2 , it would get you the text between the "|" and the "%" symbol. 
Cyndy S replied to Jonathan VH on 10-Nov-09 10:14 PM
I'm actually looking for VBA.  I have a three case function that I'm trying to create that gives either side or the middle of the string between the symbols depending on whether the user chooses 1, 2 or 3.  The function basically is like this

'Case 1
'Pipe$=Left$(text_string, len(text_string)-symbol_side-1)

'Case 2
'Pipe$=Right$(text_string, len(text_string)-symbol_side)

'Case 3
'Pipe$=Right$(text_string, len(text_string)-symbol)

depending on what the user puts in the function 1, 2 or 3 they get a different piece of what lies on either side of the symbol:  

This is what|I want to get the%user to have.

1 would get-This is what
2 would get-I want to get the
3 would get-user to have

and I need it to happen in VBA.  I have had luck with either side but still keep getting a number with case 2 instead of the middle portion.
Cyndy S replied to Jonathan VH on 10-Nov-09 10:37 PM
Thanks so much, Jonathan!  I had to step through it and figure out what went where but it worked!  Thank you so much!