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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/45175012/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-12 12:55:04  来源:igfitidea点击:

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

vbaexcel-vbaexcel

提问by Matt Hill

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.

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

Cheers!

干杯!

回答by Scott Craner

Use Split():

使用拆分():

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