Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 86
Default Parsing a text file

I have a program that I kick off out of Excel, which dumps results to a log
file. I'd like to parse the log file to see if everything worked OK. Of all
the lines that get dumped, only the bottom is important...

[lots of text]
03/30/2007 13:19:50 Loading is complete. Loading Status is as follows:
Records Read: 1241
Loaded: 1241
Processed: 1099
Failed to Load: 0
Failed to Process: 142
03/30/2007 13:19:50 Import Finished
[a little bit of text]

What I'd like to do is find the last instance of these lines, specifically
the Failed to Load and Failed to Process counts. That way after kicking off
the program, I can tell the user what happened.

So the first question is how to get the counts correctly. I can determine
whether the line has "Failed to Load:" using InStr, but how do I say "parse
out the number from the : to the end of the line"? This is something I'm not
familiar with in VBA.

The second question is whether or not I can loop backwards. Since this file
can grow very large I think performance might be an issue. If I work from the
bottom up, I can stop after processing something like a dozen lines.

Any suggestions?

Maury
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 772
Default Parsing a text file

Yes you can loop backwards, there are various methods, I will supply one. As
far as your number issue, adapt this: =RIGHT(A1,LEN(FIND(":",A1))+1) it will
return all numbers to the right of the :.

This will get you the last row in a particular column, I used A:
lastRow = Cells(Rows.Count, "A").End(xlUp).Row

then use that number in your loop or for next something like
for I = lastrow to 1 step -1
next <-not tested
--
-John Northwest11
Please rate when your question is answered to help us and others know what
is helpful.


"Maury Markowitz" wrote:

I have a program that I kick off out of Excel, which dumps results to a log
file. I'd like to parse the log file to see if everything worked OK. Of all
the lines that get dumped, only the bottom is important...

[lots of text]
03/30/2007 13:19:50 Loading is complete. Loading Status is as follows:
Records Read: 1241
Loaded: 1241
Processed: 1099
Failed to Load: 0
Failed to Process: 142
03/30/2007 13:19:50 Import Finished
[a little bit of text]

What I'd like to do is find the last instance of these lines, specifically
the Failed to Load and Failed to Process counts. That way after kicking off
the program, I can tell the user what happened.

So the first question is how to get the counts correctly. I can determine
whether the line has "Failed to Load:" using InStr, but how do I say "parse
out the number from the : to the end of the line"? This is something I'm not
familiar with in VBA.

The second question is whether or not I can loop backwards. Since this file
can grow very large I think performance might be an issue. If I work from the
bottom up, I can stop after processing something like a dozen lines.

Any suggestions?

Maury

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 35,218
Default Parsing a text file

Since there's only a couple of items to extract, I'd just use a couple of if
statements:

Option Explicit
Sub testme()

Dim iRow As Long
Dim wks As Worksheet
Dim ColonPos As Long
Dim myStr As String

Set wks = ActiveSheet

Dim LoadFailedCount As Long
Dim ProcFailedCount As Long

LoadFailedCount = -99999
ProcFailedCount = -99999

With wks
For iRow = .Cells(.Rows.Count, "A").End(xlUp).Row To 1 Step -1

myStr = LCase(Trim(.Cells(iRow, "A").Value))

If myStr Like LCase("Failed to Load:") & "*" Then
ColonPos = InStr(1, myStr, ":", vbTextCompare)
LoadFailedCount = Val(Trim(Mid(myStr, ColonPos + 1)))
ElseIf myStr Like LCase("Failed to Process:") & "*" Then
ColonPos = InStr(1, myStr, ":", vbTextCompare)
ProcFailedCount = Val(Trim(Mid(myStr, ColonPos + 1)))
End If

If LoadFailedCount = 0 _
And ProcFailedCount = 0 Then
Exit For
End If

Next iRow
End With

MsgBox LoadFailedCount & vbLf & ProcFailedCount

End Sub



Maury Markowitz wrote:

I have a program that I kick off out of Excel, which dumps results to a log
file. I'd like to parse the log file to see if everything worked OK. Of all
the lines that get dumped, only the bottom is important...

[lots of text]
03/30/2007 13:19:50 Loading is complete. Loading Status is as follows:
Records Read: 1241
Loaded: 1241
Processed: 1099
Failed to Load: 0
Failed to Process: 142
03/30/2007 13:19:50 Import Finished
[a little bit of text]

What I'd like to do is find the last instance of these lines, specifically
the Failed to Load and Failed to Process counts. That way after kicking off
the program, I can tell the user what happened.

So the first question is how to get the counts correctly. I can determine
whether the line has "Failed to Load:" using InStr, but how do I say "parse
out the number from the : to the end of the line"? This is something I'm not
familiar with in VBA.

The second question is whether or not I can loop backwards. Since this file
can grow very large I think performance might be an issue. If I work from the
bottom up, I can stop after processing something like a dozen lines.

Any suggestions?

Maury


--

Dave Peterson
  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 86
Default Parsing a text file

"John Bundy" wrote:

This will get you the last row in a particular column, I used A:
lastRow = Cells(Rows.Count, "A").End(xlUp).Row


But this only works if the file fits into Excel and can be loaded up. In
this example the file is very often too large to be loaded because the number
of lines is 64k

But for testing purposes I just went ahead and did it the brute force way.
It takes well under 1 second! There's times when VBA does manage to impress
me, and this is one of those times.

Maury
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Parsing a Text file into worksheet with VBA [email protected] Excel Programming 3 October 25th 06 02:51 AM
Parsing a Text File [email protected] Excel Programming 8 January 30th 06 04:43 PM
Issue with parsing text file into worksheet primrose Excel Programming 1 May 25th 05 05:09 PM
VBA Import of text file & Array parsing of that data Dennis Excel Discussion (Misc queries) 4 November 28th 04 10:20 PM
Parsing imported text file with macro... help! scrupul0us[_2_] Excel Programming 0 September 7th 04 10:13 PM


All times are GMT +1. The time now is 02:02 AM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"