Use Excel VBA To Open A Text File and Search It For A Specific String
Use Excel VBA To Open A Text File and Search It For A Specific String
Quick access
Answered by: Use Excel VBA to open a text file and search it for a
specific string?
Microsoft ISV Community Center forums > Visual Basic for Applications (VBA)
Question
Hans Vogelaar MVP I need to use Excel VBA to open a text file (easy enough), but then search the text file
MCC, MVP for a specific string. It looks like the TextorProperty/.FileSearch capacities would have
www.eileenslounge.com taken care of this, but it was deprecated starting with Microsoft Office 2007. I've tried
60,630 Points 5 11 0
several other ways, using "StreamReader" or making a new FileSystemObject, but my
I've been using
version of Excel won't let me add any references, so neither of these worked. Any
3 Sign in
Excel since 1986 to vote suggestions?
(version 1.0 for the Macintosh),
Word and Access since the mid Tuesday, July 02, 2013 8:47 PM
'90s. I have been an MVP for Access
Reply | Quote | laura_fedora 0 Points
since October 1, 2010.
Hans Vogelaar MVP's threads (last
60 days)
View Profile
Answers
http://social.msdn.microsoft.com/Forums/en-US/62fceda5-b21a-40b6-857c-ad28f12c1b23/use-excel-vba-to-open-a-text-file-and-search-it-for-a-specific-string?f 1/3
1/20/2014 Use Excel VBA to open a text file and search it for a specific string?
Sub SearchTextFile()
Const strFileName = "C:\MyFiles\TextFile.txt"
Const strSearch = "Some Text"
1 Dim strLine As String
Sign in
to vote Dim f As Integer
Dim lngLine As Long
Dim blnFound As Boolean
f = FreeFile
Open strFileName For Input As #f
Do While Not EOF(f)
lngLine = lngLine + 1
Line Input #f, strLine
If InStr(1, strLine, strSearch, vbBinaryCompare) > 0 Then
MsgBox "Search string found in line " & lngLine, vbInformation
blnFound = True
Exit Do
End If
Loop
Close #f
If Not blnFound Then
MsgBox "Search string not found", vbInformation
End If
End Sub
All replies
http://social.msdn.microsoft.com/Forums/en-US/62fceda5-b21a-40b6-857c-ad28f12c1b23/use-excel-vba-to-open-a-text-file-and-search-it-for-a-specific-string?f 2/3
1/20/2014 Use Excel VBA to open a text file and search it for a specific string?
Sub SearchTextFile()
Const strFileName = "C:\MyFiles\TextFile.txt"
Const strSearch = "Some Text"
1 Dim strLine As String
Sign in
to vote Dim f As Integer
Dim lngLine As Long
Dim blnFound As Boolean
f = FreeFile
Open strFileName For Input As #f
Do While Not EOF(f)
lngLine = lngLine + 1
Line Input #f, strLine
If InStr(1, strLine, strSearch, vbBinaryCompare) > 0 Then
MsgBox "Search string found in line " & lngLine, vbInformation
blnFound = True
Exit Do
End If
Loop
Close #f
If Not blnFound Then
MsgBox "Search string not found", vbInformation
End If
End Sub
United States (English) Newsletter Privacy & cookies Terms of Use Trademarks 2014 Microsoft
http://social.msdn.microsoft.com/Forums/en-US/62fceda5-b21a-40b6-857c-ad28f12c1b23/use-excel-vba-to-open-a-text-file-and-search-it-for-a-specific-string?f 3/3