text in file

  • Thread starter Thread starter codie
  • Start date Start date
C

codie

I am looking for a fast and efficient method to verify if a text
string exists in a file. Has anyone created anything like this?

The files are typically under 100K but I will be doing thousands of
checks.
 
You can try this

Sub Test()

Dim i As String
i = InputBox("Find:", "Search For Value")
Cells.Find(What:=i, After:=Range("A1"), LookIn:=xlFormulas, LookAt
_
:=xlPart, SearchOrder:=xlByRows,
SearchDirection:=xlNext).Activate
If ActiveCell.Value = i Then MsgBox i & " was found in cell " &
ActiveCell.Address


End Sub
 
Modified it to search your whole workbook. Ofcourse you can hardcode
the string you would like to search for and remove the input box, and
also modify the way you would like to identify the found results.

Sub Test()
Dim i As String
Dim n As Single

i = InputBox("Find:", "Search For Value")
For n = 1 To Sheets.Count
Sheets(n).Select 'Activate
On Error Resume Next
With cells
.Find(What:=i, After:=Range("A1"), LookIn:=xlFormulas, LookAt
_
:=xlPart, SearchOrder:=xlByRows,
SearchDirection:=xlNext).Activate
End With
If ActiveCell.Value = i Then
MsgBox i & " was found in cell " & ActiveCell.Address
Exit Sub
Else
End If
Next n


End Sub
 
Oops, sorry. I needed to mention that the file being searched is a
simple text file.
 
Use native VBA functions or the FileSystemObject to read the file into a
string variable, then use instr() to check for your text.

Tim
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top