Excel从文本parsing出一个数字列表(一个单元格中的几个数字)

我需要从excel文本中parsing出一个跟踪数字列表。 字符的位置并不总是相同的。 一个例子:

Location ID 987 Your package is arriving 01/01/2015 Fruit Snacks 706970554628 <http://www.fedex. com/Tracking?tracknumbers=706970554628> Olive Oil 709970554631 <http://www.fedex. com/Tracking?tracknumbers=709970554631> Sign 706970594642 <http://www.fedex .com/Tracking?tracknumbers=706970594642> Thank you for shopping with us! 

大块文本位于一个单元格中。 我想结果要么是3个单独的列或行看起来像这样:

706970554628,709970554631,706970594642

跟踪号码不会总是相同的。 一个细胞可能有六个而另一个细胞有一个。

感谢您的任何帮助!!

我想你会需要一些VBA来做到这一点。 这不会是超级简单的东西。 加里的学生有一个伟大的例子,从一个大string中获取数字。 如果你需要一些更具体的场景,你需要逐字parsingstring,并在URL中遇到跟踪号码时找出它。

像下面这样的事情将会诀窍:

 Function getTrackingNumber(bigMessage As String, numberPosition As Integer) As String Dim intStrPos As Integer Dim arrTrackNumbers() As Variant 'create a variable to hold characters we'll use to identify words Dim strWorkSeparators As String strWordSeparators = "()=/<>?. " & vbCrLf 'iterate through each character in the big message For intStrPos = 1 To Len(bigMessage) 'Identify distinct words If InStr(1, strWordSeparators, Mid(bigMessage, intStrPos, 1)) > 1 Then 'we found the start of a new word 'if foundTrackNumber is true, then this must be a tracking number. Add it to the array of tracking numbers If foundTrackNumber Then 'keep track of how many we've found trackNumbersFound = trackNumbersFound + 1 'redim the array in which we are holding the track numbers ReDim Preserve arrTrackNumbers(0 To trackNumbersFound - 1) 'add the track arrTrackNumbers(trackNumbersFound - 1) = strword End If 'Check to see if the word that we just grabbed is "tracknumber" If strword = "tracknumbers" Then foundTrackNumber = True Else foundTrackNumber = False End If 'set this back to nothing strword = "" Else strword = strword + Mid(bigMessage, intStrPos, 1) End If Next intStrPos 'return the requested tracking number if it exists. If numberPosition > UBound(arrTrackNumbers) + 1 Then getTrackingNumber = "" Else getTrackingNumber = arrTrackNumbers(numberPosition - 1) End If End Function 

这是一个UDF,所以你可以在工作表中使用它作为公式:

  =getTrackingNumber(A1, 1) 

这将返回单元格A1中遇到的第一个跟踪号码。 结果公式

  =getTrackingNumber(A1, 2) 

将返回第二个跟踪号码,依此类推。

这并不是一个快速的function,尽pipe它逐字地parsing了大string,并且随时做出决定。 如果你能把Gary的学生的答案变成可行的话,那么在更大的数据上就会更快,CPU占用也更less。 但是,如果你得到的结果太多,需要像外科医生那样去做,那么这应该让你进入球场。

如果跟踪始终是一个12位的数字 ,那么select单元格run run this short macro:

 Sub parser117() Dim s As String, ary, i As Long With ActiveCell ary = Split(Replace(Replace(.Text, Chr(10), " "), Chr(13), " "), " ") i = 1 For Each a In ary If Len(a) = 12 And IsNumeric(a) Then .Offset(0, i).Value = a i = i + 1 End If Next a End With End Sub 

在这里输入图像说明