✔ 最佳答案
Please try following function:
Function GetToken(ByVal sVal As String, sDel As String, nPos As Integer) As String
Dim nPos1, nSet As Integer
Dim i As Integer
nPos1 = 1
nSet = 0
For i = 1 To Len(sVal) - Len(sDel) + 1
If Mid(sVal, i, Len(sDel)) = sDel Then
nSet = nSet + 1
If nSet = nPos Then
GetToken = Mid(sVal, nPos1 + IIf(nSet = 1, 0, Len(sDel)), i - nPos1 - IIf(nSet = 1, 0, Len(sDel)))
Exit Function
End If
nPos1 = i
End If
Next
If nPos > 1 And nSet + 1 = nPos Then
GetToken = Mid(sVal, nPos1 + Len(sDel), Len(sVal) - nPos1 - Len(sDel) + 1)
Else
GetToken = ""
End If
End Function
And check with following code:
Sub ChkGetTok()
MsgBox GetToken("123**456**789**000", "**", 1)
MsgBox GetToken("123**456**789**000", "**", 2)
MsgBox GetToken("123**456**789**000", "**", 3)
MsgBox GetToken("123**456**789**000", "**", 4)
MsgBox GetToken("123**", "**", 1)
MsgBox GetToken("**123**", "**", 2)
MsgBox GetToken("444**13646546", "**", 1)
MsgBox GetToken("123165468**111**5646489986", "**", 2)
End Sub