我想你會需要一些VBA來做到這一點。這不會是超級簡單的東西。加里的學生有一個很好的例子,從一大串字符串中獲取數字。如果您需要更具體的場景,您需要逐字解析字符串,並在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資源。但是,如果你得到的結果太多,需要像外科醫生那樣去做,那麼這應該讓你進入球場。
此代碼非常整潔! .....................我可以自己使用它。 –
謝謝@加里的學生我仍然不相信這是最好的。一些重構肯定已經成熟。我可能會用'strWordSeparators'作爲輸入的迭代'replace()'函數包裝。如果速度足夠快,那麼將輸出傳遞給'split()'將會非常棒,因爲迭代一組單詞會快得多,那麼它將是一串字符。特別適用於大型傳入字符串 – JNevill
我同意,但即使是發佈的代碼也非常好 –