您可以使用Excel中內置的「Text to Columns」功能來實現此功能。
突出顯示列A
並轉到Data >> Text To Columns。選擇「分隔符」,然後選擇「空格」作爲分隔符。單擊完成,它將按空間拆分數據並將其寫出到相鄰的列。
對於VBA UDF可以使用超級方便SPLIT()
字符串的方法將由分隔符分割字符串數組。然後,只需摘掉您需要的位置:
Public Function strtok(strIn As String, delim As String, token As Integer) As String
'Split the <strIn> by the <delim> outputting the string at position <token>
strtok = Split(strIn, delim)(token - 1)
End Function
要在工作表中使用此:
=strtok(A1," ", 1)
當空間分割這將在您的A1
值的第一個位置返回字符串。如果你把這個B1
那麼你可以使用Column()
,使之更加動態的,所以你可以拖動公式向右和向下:
=strtok($A1," ",Column()-1)
這裏是一個編輯上述funcion將由分裂DELIM,然後從分割重建只用數字值的數組,然後輸出從數字陣列請求令牌:
Public Function strtok(strIn As String, delim As String, token As Integer) As String
'Split the strIn by the delim outputting the string at position <token>
Dim strArr As Variant 'array to split the initial string
Dim numArr() As Double 'array to hold just numeric values
Dim arrEl As Variant 'Element for the array
'fill strArr
strArr = Split(strIn, delim)
'initial numArr()
ReDim numArr(0 To 0)
'Loop through each element in the strArr
For Each arrEl In strArr
If IsNumeric(arrEl) Then
'resize the numArr to hold a new value
If numArr(0) <> 0 Then ReDim Preserve numArr(0 To UBound(numArr) + 1)
'write the value
numArr(UBound(numArr)) = arrEl
End If
Next arrEl
strtok = numArr(token - 1)
End Function
您可以通過使用標籤或分隔符在Excel中使用文本列功能「」而不是寫一個宏。 – Histerical
儘管如此 - 可能是「文本到列」可能是一個好主意(你似乎有空白和冒號作爲分隔符......任何其他???)...什麼是有效的手機號碼...任何數字字符串...只有那些不以9999開頭的...其他? – MikeD