• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 401
  • Last Modified:

Find the first space in a string and only return the characters before that space using vba

Is there an easy way to find in a string and only return the characters before the first space in the string.

For example: married  divorce single blabla

should return married in this example

many thanks!!
0
BauwensER
Asked:
BauwensER
1 Solution
 
mbizupCommented:
Try this:


Dim strYourString as string
strYourString = "married divorced etc"

msgbox Split(strYourString," ") (0)
0
 
peter57rCommented:
If you want this ina query then...

Left(yourfield , instr(yourfield & " ", " ")-1)
0
 
baglassCommented:
The following will break up the source string [the first argument to Split()] using the second argument as the delimiter.  
The delimter may be more than one character in length.  
If the source string is empy then the size of the array will be -1.
If the delimter is not found then the whole string is returned in element 0 of the array.
'---------------------------------------------------------------------------
Dim mystring() As String

Sub testme()

   mystring = Split("this is a sample string", " ")
   if ubound(mystring) >= 0 then
      Debug.Print mystring(0)
   endif

End Sub
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now