Extract the nth word from a string

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hello,
How can I use the built-in functions to extract the nth word in any string?
Words in the string are separated by spaces.
Thanks.
David
 
David said:
Hello,
How can I use the built-in functions to extract the nth word in any
string? Words in the string are separated by spaces.
Thanks.
David

If the words are separated only by single spaces -- not more than one
consectuive space -- then something like this should work:

strWordN = Split(strMyString, " ")(N - 1)

Note that this will raise an error if there are not at least N words in
the string. It's probably a good idea to expand this simple line into a
more complex VBA function, with bounds-checking and error-handling.
 
Back
Top