我會推薦使用InStr函數。我用這種方法取得了巨大的成功。
Public Function FileImport()
Dim searchStart As String, searchEnd As String, fullString As String, stringEnd As Long, startLoc As Long, endLoc As Long
searchStart = "5DEL"
searchEnd = "2FRM"
'File open/loop code goes somewhere and fullString is set to the data contained within. I'm manually setting this as an example below:
fullString = "2FRM Hello World! 5DEL Bad Datatype 6OTH Other Data 2FRM Insert Me 5FOR Valid Datatype"
stringEnd = Len(fullString)
If stringEnd > 0 Then 'Skip the section if the string blank.
Do
startLoc = InStr(1, fullString, searchStart, vbTextCompare) 'Find the piece of text we are looking for.
If startLoc > 0 Then 'We have found the starting location of 5DEL in the string.
endLoc = InStr(startLoc, fullString, searchEnd, vbTextCompare) 'Find end of the bad string, starting from the starting location.
If endLoc > 0 Then 'A starting and ending location were found.
fullString = Left(fullString, startLoc - 1) & Mid(fullString, endLoc, Len(fullString))
Else 'No ending location was found, thus we only need the left part of the string.
fullString = Left(fullString, startLoc - 1)
Exit Do
End If
Else
Exit Do 'The bad string was not found, exit.
End If
DoEvents 'Just so that the application does not become non-responsive. Can be removed.
Loop
End If
Debug.Print fullString
'Output is 2FRM Hello World! 2FRM Insert Me 5FOR Valid Datatype
'File open/loop code ends.
End Function
到目前爲止,您對此次嘗試有哪些代碼?看看使用'Instr'和'Mid'只是爲了初學者。 –