简体   繁体   中英

EXCEL-VBA Return highest value from a find function from a txt file VBA

Here is the problem:

-I have a text file that contains a lot of data but I have to extract the line containing the string "pressure: (values)" . The file has lots of this string and I wanted to extract the highest values after the word pressure. Here are some sample contents from the file: - pressure: 10.1101 - pressure: 20.1102 - pressure: 20.1020

The number of occurrence of "pressure: " is not fixed.

I have already figure out some needed function to make this happen (just copied and edited some script found in the net). But I don't know how to store the matched values (perhaps store in a array?) then apply max function to return the highest value. Will appreciate any input.

Other approach to attain the desired task is also welcome.

Here is my script:

Sub search()
Const ForReading = 1
Dim FSO, FileIn, strTmp

Set FSO = CreateObject("Scripting.FileSystemObject")
Set FileIn = FSO.OpenTextFile("D:\data.txt", ForReading)

Do Until FileIn.AtEndOfStream
    strTmp = FileIn.ReadLine
    If Len(strTmp) > 0 Then 'read if not blank
        If InStr(1, strTmp, "pressure:", vbTextCompare) > 0 Then 'find function
            x = Mid(strTmp, 10, 7) 'to extract the numeric values only
            MsgBox x 'just for checking if value of x is correct

            'add function that will return the highest value
            'WorksheetFunction.Max (x)

        End If

    End If
Loop

FileIn.Close

End Sub

If you only want the single largest value of pressure then simply keep track of the largest value found so far, and update it if a larger value is found.

Here's your code refactored with a few other minor issues addressed (changes marked <--- )

Sub search()
    Const ForReading = 1
    '<--- Use specific data types rather than variant
    Dim FSO As Object
    Dim FileIn As Object
    Dim strTmp As String
    Dim KeyWord As String
    Dim i As Long, j As Long
    Dim x As Double
    Dim MaxX As Double

    MaxX = -4.94065645841247E-324 '<-- initialise to lowest possible value
    KeyWord = "pressure:" ' <--- generalise the function
    Set FSO = CreateObject("Scripting.FileSystemObject")
    Set FileIn = FSO.OpenTextFile("D:\data.txt", ForReading)

    Do Until FileIn.AtEndOfStream
        strTmp = FileIn.ReadLine
        If Len(strTmp) > 0 Then 'read if not blank
            i = InStr(1, strTmp, KeyWord, vbTextCompare)
            If i Then  'find function
                ' <-- cope with possibility Pressure is not at start of string 
                '     and may have trailing data
                strTmp = Trim$(Mid$(strTmp, i + Len(KeyWord)))
                i = InStr(strTmp, " ")
                If i Then
                    strTmp = Trim$(Left$(strTmp, i - 1))
                End If
                x = Val(strTmp) 'to extract the numeric values only
                MsgBox x 'just for checking if value of x is correct

                'add function that will return the highest value
                If x > MaxX Then MaxX = x
            End If

        End If
    Loop

    FileIn.Close
    MsgBox "Max presseure is " & MaxX
End Sub

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM