Extract the nth word from a string

D

David

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
 
D

Dirk Goldgar

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top