Originally Posted by
DavidLee91
Just for education's sake, what is probably the faster way of loading the whole file and searching for 'Error'?
Again, untested, but:
Sub ReadLog2()
Dim FilePath As String
Dim strLine As String
FilePath = "D:test2.txt"
Open FilePath For Input As #1
strLine = Input(LOF(1), #1)
Close #1
myResult = "Nothing Found"
If InStr(1, strLine, "Error", vbTextCompare) > 0 Then
myResult = "Error found"
ElseIf InStr(1, strLine, "Warning", vbTextCompare) > 0 Then
myResult = "Warning found"
End If
Range("D1").Value = myResult
End Sub
Originally Posted by
DavidLee91
In addition, how can I edit the following code if I want to have more than 1 condition? For example, if the text file contains "Error", it will go range "D1" and states "Error found". If it containts "warning", it will go range "D1" and states "warning found" instead?
Try (untested of course):
Sub ReadLog()
Dim FilePath As String
Dim strLine As String
FilePath = "D:test2.txt"
Open FilePath For Input As #1
Do While EOF(1) = False
'read the next line of data in the text file
Line Input #1, strLine
If InStr(1, strLine, "Error", vbTextCompare) > 0 Then
Range("D1").Value = "Error found"
Exit Do ' once found we don't need to keep on looking.
ElseIf InStr(1, strLine, "Warning", vbTextCompare) > 0 Then
Range("D1").Value = "Warning found"
Exit Do ' once found we don't need to keep on looking.
End If
Loop
Close #1
End Sub
Whether it'll be faster or not might be difficult to gauge; if the text file is really massive then it'll be resource hungry and may slow the computer, but if it's massive and the Warning/Error is near the beginning of the file then the line by line approach might be quicker.