Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Need some code tweaking. To scan text files to find certain figures

Status
Not open for further replies.

X82

Technical User
Sep 23, 2012
4
GB
Code:
Sub test() 
    Dim myDir As String, fn As String, txt As String 
    Dim myMax As Double, myName As String, myLoc As String, n As Long 
    Dim myMatch As Object, x, y, temp As String, i As Long, ii As Long 
    myDir = ThisWorkbook.Path & "\" 
    fn = Dir(myDir & "*.txt") 
    If fn <> "" Then 
        n = n + 1 
        Sheets(1).Cells(n, 1).Resize(, 4).Value = _ 
        [{"FileName","Name","Location","Max"}] 
        With CreateObject("VBScript.RegExp") 
            .IgnoreCase = True 
            Do While fn <> "" 
                myName = Empty: myLoc = Empty: myMax = 0 
                txt = CreateObject("Scripting.FileSystemObject") _ 
                .OpenTextFile(myDir & fn).ReadAll 
                .Global = True 
                .Pattern = "[^\n]+" 
                Set myMatch = .Execute(txt) 
                .Global = False 
                For i = 0 To myMatch.Count - 1 
                    .Pattern = "\b(I(out)?Max( B1)?|Ph ?I(.A)?)(?=\t)" 
                    If .test(myMatch(i)) Then 
                        temp = .Execute(myMatch(i))(0) 
                        x = Application.Match(temp, Split(myMatch(i), vbTab), 0) 
                        myMax = Val(Split(myMatch(i + 1), vbTab)(x - 1)) 
                        Exit For 
                    Else 
                        .Pattern = "\b(IMax Ph1)(?=\t)" 
                        If .test(myMatch(i)) Then 
                            temp = .Execute(myMatch(i))(0) 
                            x = Application.Match(temp, Split(myMatch(i), vbTab), 0) 
                            For ii = x - 1 To x + 1 
                                myMax = myMax + Val(Split(myMatch(i + 1), vbTab)(ii)) 
                            Next 
                            Exit For 
                        End If 
                    End If 
                Next 
                .Pattern = "(\d{2}(?:[/\.])){2}\d{4}\t(\d{2}:){2}\d{2}\t[^\t]+\t([^\t]+)\t([^\t]+)" 
                If .test(txt) Then 
                    myName = .Execute(txt)(0).submatches(2) 
                    myLoc = .Execute(txt)(0).submatches(3) 
                End If 
                n = n + 1 
                Sheets(1).Cells(n, 1).Resize(, 4).Value = _ 
                Array(fn, myName, myLoc, myMax) 
                fn = Dir 
            Loop 
        End With 
    Else 
        MsgBox "No file found" 
    End If 
End Sub

I have a system of about 500 text files from our powerbar readings taken each night. I needed a script to run which would scan each of the text files, find a certain set of figures and put them into a spreadsheet. Above is what some kind soul did for me. It works beautifully, with one small problem which was my own. This will take you to the 4 example files of the types of file this script will scan.

It works perfectly to pick up all red information (which you will see in the link above). The problem is ,with File 2, it must scan for the two figures in red, and add them together. But I forgot to explain this to the creator who is now away for a long period. With his code it will only add the 2nd digits in red, 1.9 from file 2. I have now correctly highlighted the first set of figures I need, in this case 1.8. I looked at the code over and over and don't know where to begin to modify myself to do this. With this complete it would be perfect!

Any ideas how I can modify this code slightly to fix this error? Any help on this matter is greatly appreciated.
 
hi,

I'd be apt to IMPORT each file using the Data > Get external data > From Text.... Your files will be parsed into columns and ben much easier to grab the required data, one import querytable per import spec format.

If you want to debug your code, you can STEP thru is to discover what is happening.

faq707-4594

Skip,

[glasses]Just traded in my old subtlety...
for a NUANCE![tongue]
 
I did try the get external data, but it would take me a long time to do all 500 files. With the above code, it works on all files in about 5 seconds. The problem is as I have said, the code is slightly wrong and not pulling the correct data.
 
DEBUG to discover what is happening.

Skip,

[glasses]Just traded in my old subtlety...
for a NUANCE![tongue]
 
I've tried debugging best I can, I am an amateur at this, but I can't seem to fathom it out. A certain line of code is telling it to scan the txt file, find a value and display it. I need it to find one more value and add it to the first. How I can do this alludes me. I've stepped into the code and just hit F8 to scan through the code but overall it doesn't tell me what its doing.
 
you can also Debug.Print variables too.

Skip,

[glasses]Just traded in my old subtlety...
for a NUANCE![tongue]
 
I think I will have to give up. I have no idea what I am looking at, making my eyes go blurry. Someone can look at this code and tell me exactly what I need, I look at it and all I see is gibberish.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top