I am trying to search through large log files to find a string of text, then if that string is present to find another string of text and then return the next 5 lines of data. I have managed to search the text file for the string and return the 5 lines after however I cannot seem to get the macro to search for both lines of text before returning the 5 lines.
For example if the text file looked like this:
17:42:56: Log File Closed
17:42:56: PrintInvoice: 2
17:42:56: copyReportData:
17:42:56: getNextRptDataID:
17:42:58: CalcDelCharge:
17:42:58: Sub Total: 3.80
17:42:58: Del Total: 0.00
17:42:58: Disc Total: 0.00
17:42:58: Vat Total: 0.00
17:42:58: Inv Total: 3.80
18:33:00: CalculateAmtDue:
18:33:00: CalculateChange:
18:33:00: UpdateDelCharge:
18:33:00: UpdateTotals
18:42:58: CalcDelCharge:
18:42:58: Sub Total: 5.80
18:42:58: Del Total: 0.00
18:42:58: Disc Total: 0.00
18:42:58: Vat Total: 0.00
18:42:58: Inv Total: 5.80
I want to extract the 5 lines after the first 'CalcDelCharge' as it is follows 'PrintInvoice: 2', which is one of the strings I am also wanting to search for.
The text file contains 'CalcDelCharge' throughout, however I am only interested in the instances when it comes after 'PrintInvoice: 2', which appears much less frequently.
Here is what I have so far
Dim fn As String, txt As String, delim As String, a() As String
Dim i As Long, ii As Long, iii As Long, x, y
fn = "C:\Documents\tilllogfile.log"
delim = vbTab
temp = CreateObject("Scripting.FileSystemObject").OpenTextFile(fn).ReadAll
x = Split(temp, vbCrLf)
ReDim a(1 To UBound(x) + 1, 1 To 100)
For i = 0 To UBound(x)
If InStr(1, x(i), "CalcDelCharge", 1) Then
For ii = 0 To 5
n = n + 1: y = Split(x(i + ii), delim)
For iii = 0 To UBound(y)
a(n, iii + 1) = y(iii)
Next
Next
End If
This will extract 5 lines after all 'CalcDelCharge' and put it into a spreadsheet for me, I have not been able to narrow it down to the instances when it follows 'PrintInvoice: 2'.
Any help would be greatly appreciated.
Thanks.


