简体   繁体   中英

Reading a specific part of a text line using VBA

I am trying to get the 827th line from a text file to write in a cell. There are a lot of files like this so that is why I am trying to use a macro. The text file looks like this

"Drag Convergence"
"Iterations" "cd"
1     7.74776e-01
2     6.51021e-01
3     5.58885e-01
.....
824     3.57617e-01
825     3.57617e-01

I just want to write the number "3.57617e-01" to a cell. I can do the cell arrangement myself but I did not have a good way to read that value and then write it to a cell lets say (1,1)

My file location is

strFile = "D:\Analiz\Database\NACA63220_" & Mach(k) & Alpha(j) & Letter(i) & ".txt"

What I did was using

strPath = "D:\Analiz\Database\"
strExt = ".txt"
strSection = "Lift Convergence"
strValue = "825     "

With shtResult
     .Cells(1,1).Value = strValue
End With

strFile = "D:\Analiz\Database\NACA63220_" & Mach(k) & Alpha(j) & Letter(i) & ".txt"

Set data=shtSource.QueryTables.Add(Connection:TEXT;" & strPath & strFile, Destination:=shtSource.Cells(1, 1))

With data
.TextFileStartRow = 1
.TextFileParseType = xkDelimited
.TextFileVonsecutiveDelimeter = False
.TextFileTabDelimiter = False
.Text FileSemicolonDelimiter = False
.TextFileCommaDelimiter = False
.TextFileSpaceDelimiter = False
.TextFileColumnDataTypes = Array(1)
.TextFileTrailingMinusNumbers = True

.Refresh BackgroundQuery:=False
End With

Set fndSection = data.ResultRange.Find(strSection)
Set fndValue = data.ResultRange.Find(strValue, fndSection)
shtResult.Cells(shtResult.Rows.Count, 1).End(xkUp).Offset(1).Value = Replace(findValue, strValue, "")

This gives a run time error 1004, and when I press debug, it highlights the line with .Refresh BackgroundQuery

I did not put everything here like dimensioning etc because I have to use my phone to access this site so I am writing all of the codes with my phone again.

Edit: I added more lines, the problem highlights the Refresh Background Query line when I press debug. I was actually trying to implement this by tweaking to my problem:

Importing data from multiple text files into Excel VBA

use this function to read specific line from textfile

    ' read specific line no.
    ' RESULT : searched text (string)
    Function ReadLine(FilePath, LineNumber) As String
    Dim i As Integer, count As Long
    Dim strLine As String
    i = FreeFile
    Open FilePath For Input As #i
    count = 0

    While Not EOF(i)
        Line Input #i, strLine
        count = count + 1
        If count = LineNumber Then
            ReadLine = strLine
            Close #i
            Exit Function
        End If
    Wend

Close i
End Function

have you got any others problem with implementation? please clarify

the second code is for string splitting

     Function BreakString(a As String, pos As Integer) As String
     Dim WrdArray() As String
     WrdArray() = Split(a, "     ")
     BreakString = WrdArray(pos)
     End Function

Dim Text, Part1, Part2 As String
Text = "827     3.57617e-01"
Part1 = BreakString(CStr(Text), 0)
Part2 = BreakString(CStr(Text), 1)

I'd advise you not to use standard VBA file handling for this matter: VBA file handling starts reading a file from the first line, and continues, line by line, until it reaches the line you're looking for. As you need to read 827 lines, you are waiting 827 time too long for your response.

Instead of this, I'd try to find a commandline, which can handle this, something like:

tail -1 <filename> >output.txt (or head -827 <filename> | tail -1 >output.txt)

Translate this into Excel VBA, something like:

Shell "tail -f <filename> >output.txt" //pseudo-code

And then, use VBA standard text handling for reading output.txt.

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