我在某些单元格A1,A2,A3,A4中有非结构化数据,并希望在不同的单元格中提取移动数据
--------------------------(A)--------------------------
(1) `ABCDEFG CFGHJKL 9810642882 9212451029 9818682274`
(2) `ABCDERFT 9910077711 9811195125 9999966744`
(3) `ADFRTYU.9810851029 9818218521 9811056189`
(4) `ADFGT FTYUH 9873155802`
来自单元格A1的结果:
----(A)--- ----(B)--- ----(C)---
(1) 9810642882 9212451029 9818682274
答案 0 :(得分:3)
对于仅公式解决方案,在单元格B1中并上下复制:
=TRIM(MID(SUBSTITUTE(TRIM(MID($A1,MIN(FIND({1,2,3,4,5,6,7,8,9,0},$A1&1234567890)),999))," ",REPT(" ",999)),999*(COLUMN(A1)-1)+1,999))
答案 1 :(得分:2)
您可以使用Excel内置的" Text to Columns"这个功能。
突出显示列A
并转到数据>>文本到列。选择"分隔"然后" Space"作为你的分隔符。单击“完成”,它将按空格分割数据并将其写入相邻列。
对于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)
这是对上面的函数的编辑,它将被delim拆分,然后使用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>
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