Sunteți pe pagina 1din 3

1/20/2014 Use Excel VBA to open a text file and search it for a specific string?

MSDN subscriptions Get tools Sign in

Home Opportunity Platform Connect Downloads Library Samples Join us

Tech Advisors Perspectives Events Forums

Ask a question Search related threads Search forum questions

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

Top related threads


Search for a particular string in a
text file using VBA
Exporting Data using VBA to an
Existing Excel File
Modify a Text file using VBA with
FileSystemObject
Using VBA to search a text file
and then pull some values and
put them into an excel file
Cannot Open Text file(AJ.txt) in
to Excel workBook using a VBA
Macro in .xlsm file

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?

Try something like this:

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

Regards, Hans Vogelaar

Marked as answer by laura_fedora Wednesday, July 03, 2013 1:31 PM

Tuesday, July 02, 2013 9:13 PM

Reply | Quote | Hans Vogelaar MVP (MCC, MVP) 60,630 Points

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?

Try something like this:

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

Regards, Hans Vogelaar

Marked as answer by laura_fedora Wednesday, July 03, 2013 1:31 PM

Tuesday, July 02, 2013 9:13 PM

Reply | Quote | Hans Vogelaar MVP (MCC, MVP) 60,630 Points

Thanks so much, that worked!

Wednesday, July 03, 2013 1:41 PM

0 Reply | Quote | laura_fedora 0 Points


Sign in
to vote

Dev centers Learning resources Community Support


Microsoft Virtual Academy Forums Self support
Windows
C hannel 9 Blogs Other support options

Windows Phone Interoperability Bridges C odeplex


MSDN Magazine
Office

Windows Azure Programs


BizSpark (for startups)
Visual Studio DreamSpark
Faculty C onnection
More... Microsoft Student

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

S-ar putea să vă placă și