vba - How to return only text after a comma in a string -


i extracting text between parens in active window title bar. part working great (thanks received here previously!). want create 2 separate macros - 1 returns first name, , other returns last name.

my active window title bar looks this:

some text left (henderson,tom) text right (there no space after comma)

the last name macro works perfectly. looks this:

sub a1lastname()     'extract last name of patient title bar (between parens)     dim strpatientname string     dim openposition integer '(open paren marker)     dim closeposition integer '(close paren marker)     openposition = instr(activedocument.activewindow.caption, "(")     closeposition = instr(activedocument.activewindow.caption, ")")     strpatientname = mid(activedocument.activewindow.caption, _         openposition + 1, closeposition - openposition - 1)     dim c long     c = instr(strpatientname, ",")     strpatientname = left(strpatientname, c - 1)     selection.typetext strpatientname end sub 

the second macro identical first, except second-to-last line of code has "right" instead of "left" instruction:

sub a1firstname()     'extract first name of patient title bar (between parens)     dim strpatientname string     dim openposition integer '(open paren marker)     dim closeposition integer '(close paren marker)     openposition = instr(activedocument.activewindow.caption, "(")     closeposition = instr(activedocument.activewindow.caption, ")")     strpatientname = mid(activedocument.activewindow.caption, _         openposition + 1, closeposition - openposition - 1)     dim c long     c = instr(strpatientname, ",")     strpatientname = right(strpatientname, c - 1)     selection.typetext strpatientname end sub 

here's problem: "first name" macro returns last name minus first 4 characters, followed first name, instead of first name.

the examples i'm able find anywhere in google land excel. have combined through vba manuals, , give similar examples have used extracting text right of character.

what doing wrong?

you can use split() create array out of comma-separated parts of text, access either first or second part:

sub a1lastname()     dim strpatientname string     strpatientname = parenscontent(activedocument.activewindow.caption)     if strpatientname "*,*"         selection.typetext trim(split(strpatientname, ",")(0))     end if end sub   sub a1firstname()     dim strpatientname string     strpatientname = parenscontent(activedocument.activewindow.caption)     if strpatientname "*,*"         selection.typetext trim(split(strpatientname, ",")(1))     end if end sub  'utility function: find , return text enclosed () '   return empty string if no () found function parenscontent(txt) string     dim rv string, pos long, pos2 long     if txt "*(*)*"         pos = instr(1, txt, "(")         pos2 = instr(pos, txt, ")")         rv = mid(txt, pos + 1, (pos2 - pos) - 1)     end if     parenscontent = rv end function 

Comments