如何从VBA(Excel)中的字符串中提取第一个单词?

Mat*_*ill 2 excel vba excel-vba

例如,如果我有:

Sub TestModule()
    Dim test As String
    test = "Machine Head"
End Sub
Run Code Online (Sandbox Code Playgroud)

如何提取单词Machine并将其分配给变量?我尝试过使用Search和Left功能,但没有取得多大成功.

干杯!

Sco*_*ner 7

使用Split():

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

  • @MattHill点击asnwer的复选标记,不要忘记标记为正确. (2认同)