user7983290
user7983290

Reputation:

How to extract the first word from a string in VBA (Excel)?

For example, if I had:

Sub TestModule()
    Dim test As String
    test = "Machine Head"
End Sub

How would I extract the word Machine and assign it to a variable? I have tried using the Search and Left functions but have not had much success.

Cheers!

Upvotes: 10

Views: 23109

Answers (1)

Scott Craner
Scott Craner

Reputation: 152505

Use Split():

Sub TestModule()
    Dim test As String
    dim frstWrd as string
    test = "Machine Head"
    frstWrd = split(test," ")(0)
    Debug.Print frstWrd
End Sub

Upvotes: 18

Related Questions