我需要从excel中的文本中解析出跟踪号码列表。字符方面的位置并不总是相同的。一个例子:
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
跟踪号码的数量并不总是相同。一个单元格可能有六个,而另一个单元格有一个。
感谢您的帮助!!
答案 0 :(得分:2)
我认为你需要一些VBA才能做到这一点。而且它不会是超级简单的东西。 @ Gary'sStudent有一个从大字符串中抓取数字的好例子。如果您需要某些特定于您的场景的内容,则必须逐字解析字符串,并在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)
将返回第二个跟踪号,依此类推。
这不是一个快速的功能,因为它逐个字符地解析大字符串并做出决定。如果你可以将Gary的学生答案变成可行的,那么对于更大的数据来说,它会更快,更少CPU密集。但是,如果你得到的结果太多而且需要像外科医生那样去做,那么这应该让你进入大球场。
答案 1 :(得分:1)