这会将所有内容都放在空间的左侧:
Sub myTxt()
'Set the strings according to your post
myTxt1 = "2513,82 alpha"
myTxt2 = "999,71 somekindofexpression"
myTxt3 = "55,7 orange"
'Split the strings to an array using a space as the delimiter then assign the first element to the variable
myTxt1 = Split(myTxt1, " ")(0)
myTxt2 = Split(myTxt2, " ")(0)
myTxt3 = Split(myTxt3, " ")(0)
'Display the results
MsgBox "myTxt1 = " & myTxt1 & vbLf & "myTxt2 = " & myTxt2 & vbLf & "myTxt3 = " & myTxt3
End Sub
将 0 改为 1 以获取下一组数据,直到遇到另一个空格。您可以继续增加数字,直到它用完文本块。要找到最大块使用 ubound(Split(myTxt1, " "))
如果你一心想要使用 left 函数,你可以使用 instr (In String) 找到空格的数字 char:
instr(1,myTxt1," ")
然后您可以将它与左函数结合起来,如下所示:
Left(myText,instr(1,myTxt1," ")-1) 'Remove 1 to get rid of the space from the returned string
最后,您可以在此处使用数组来允许可扩展的输入量,如下所示:
Sub myTxt2()
Dim myTxt As Variant, X As Long
'Input your data to an array (Comma seperate your values)
myTxt = Array("2513,82 alpha", "999,71 somekindofexpression", "55,7 orange")
'Loop through the array one element at a time
For X = LBound(myTxt) To UBound(myTxt)
'Replace the element with just the first chunk of the value
myTxt(X) = Split(myTxt(X), " ")(0)
Next
'Display results
MsgBox Join(myTxt, vbLf)
End Sub
您的数据仍然可以访问,但不是 myTxt1、myTxt2、myTxt3,而是现在分别是 myTxt(1)、myTxt(2)、myTxt(3)
希望这对您现在和将来有所帮助。