vba - Find and delete text within a cell in excel -


i have pasted text in cell a1 may this:

"vanguard - total market - composite" or "vanguard - total market - commingled"

i want cut off " - composite" or " - commingled" , return rest in cell b1. i'm using formula in b1:

=left(a1,find(" - composite",a1)-1)

however, can't figure out how multiple terms (i.e. composite or commingled). there formula accomplish this? if not, how in vba?

thanks help!

if understand correctly, you're looking strip past second occurrence of -, i.e. returning trimmed (extra whitespace removed) text left of first - character. adapting this solution locate last word in string, it:

=trim(substitute(left(a1,len(a1)-len(mid(a1,find(char(1),substitute(a1,"-",char(1),len(a1)-len(substitute(a1,"-",""))))+1,len(a1)))),"-","",2)) 

this formula work or without spaces around -, , regardless of follows:

formula in action


if spacing important, can wrap substitute functions:

=substitute(substitute(trim(substitute(left(a1,len(a1)-len(mid(a1,find(char(1),substitute(a1,"-",char(1),len(a1)-len(substitute(a1,"-",""))))+1,len(a1)))),"-","",2)),"-"," - "),"  "," ") 

and have clean, identically-formatted output cases:

even spacing


Comments

Popular posts from this blog

angular - Is it possible to get native element for formControl? -

unity3d - Rotate an object to face an opposite direction -

javascript - Why jQuery Select box change event is now working? -