-
Solved: Need help in searching last occurrence of string in text file
Hi ,
I am not good in file handling in excel macro..i need some help from
u.
I have one huge text file and i want to search last occurrence of
string in that file and then cut the data after that searched line
till end of file and paste it into a new text file.
Any help appreciated. thanks in advance.
-
You have actually asked for a couple of tasks, I broke them down in the example. Post back if you have questions:
[VBA]Option Explicit
Public Sub Example()
Dim strFileText As String
Dim strFinalValue As String
strFileText = GetFileText("C:\Test\MyTextFile.txt")
strFinalValue = ExtractTextAfter(strFileText, "bar", vbTextCompare)
MsgBox strFinalValue
End Sub
Private Function GetFileText(ByVal filePath As String) As String
Dim lngFileNum As Long
Dim lngFileLen As Long
Dim strFileText As String
lngFileNum = FreeFile
Open filePath For Binary Access Read Shared As #lngFileNum
lngFileLen = FileLen(filePath)
strFileText = String$(lngFileLen, vbNullChar)
Get #lngFileNum, , strFileText
Close #lngFileNum
GetFileText = strFileText
End Function
Private Function ExtractTextAfter( _
ByVal stringValue As String, _
ByVal subStringValue As String, _
Optional ByVal compare As VbCompareMethod = VbCompareMethod.vbBinaryCompare _
) As String
Dim lngDPos As Long
Dim strRtnVal As String
lngDPos = InStrRev(stringValue, subStringValue, -1, compare)
If lngDPos Then
strRtnVal = Mid$(stringValue, lngDPos + Len(subStringValue))
End If
ExtractTextAfter = strRtnVal
End Function[/VBA]
Cordially,
Aaron
Keep Our Board Clean! - Please Mark your thread "Solved" if you get an acceptable response (under thread tools).
- Enclose your code in VBA tags then it will be formatted as per the VBIDE to improve readability.
-
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules