vba 请参阅文本文件中搜索文本下方的行

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14106593/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 19:01:44  来源:igfitidea点击:

Refer to line below search text in text file

excelvba

提问by rayray

I have data such as the following in a text file:

我在文本文件中有如下数据:

Member A
      Diameter      60 in
      Thickness     1  in
      Yield Stress  50 ksi

Brace B
      Diameter      54 in
      Thickness     1  in
      Yield Stress  50 ksi

I need to extract numerical diameter (or thickness, or yield stress) when a string of text "Member A" is found within a long text file. Data is always in same order.

当在长文本文件中找到一串文本“成员 A”时,我需要提取数字直径(或厚度或屈服应力)。数据总是以相同的顺序排列。

I can extract data that is on the same line as the text I'm searching for using "Trim" / "Mid". I do not know how to refer to "the line below" the text I'm searching for.

我可以使用“Trim”/“Mid”提取与我正在搜索的文本位于同一行的数据。我不知道如何引用我正在搜索的文本的“下面的行”。

My code:

我的代码:

Sub jtdtlextract()
    Dim str, str1, strOutPut, strBrcAngle, strComnJt, strChrdDia As String
    Dim FileToOpen, FileConverted, strRun, lngReturn, fs, f, s, ff

    FileToOpen = Application.GetOpenFilename("All Files (*.*), *.*")
    If FileToOpen <> False Then
        MsgBox FileToOpen, 0, "Open File"
    End If

    Set fs = CreateObject("Scripting.FileSystemObject")
    Set f = fs.GetFile(FileToOpen)

    FileConverted = UCase(f.ParentFolder.Path) & "\jt_dtls_extracted.txt"

    Open FileToOpen For Input Access Read Shared As #1
    Open FileConverted For Output Access Write Shared As #2

    Do Until EOF(1)
        Line Input #1, str
        str1 = LTrim(str)
        If Left(str1, 31) = "Detailed Review Report of Joint" Then
             strComnJt = Trim(Mid(str1, 35, 4))
             strOutPut = "Common_Jt" & Space(1) & strComnJt
             Print #2, strOutPut
        End If
                'I have a lot more information to extract from the text file
                'I was hoping to use a method similar to above since it's
                'fairly simple and I have no coding experience, the code
                'above only works when the information needed is on the
                'same line as the information searched for. Was written
                'by someone else.
    Loop
    Close #1
    Close #2

    strRun = "Notepad.exe " & FileConverted
    lngReturn = Shell(strRun)

End Sub

回答by Pragmateek

You'll need some state-management code to keep track of where you are in the file.

您将需要一些状态管理代码来跟踪您在文件中的位置。

If the file is not too big you don't need full-streaming and can use full-bufferingin an array.

如果文件不是太大,则不需要全流处理,可以在数组中使用全缓冲

Then you can naturally scan this array using simple indexing :

然后你可以使用简单的索引自然地扫描这个数组:

name = lines(i)
diameter = parseDiameter(lines(i+1))
thickness = parseThickness(lines(i+2))
yieldStress = parseYieldStress(lines(i+3))
i = i + 5

回答by david

 Do Until EOF(1)
    Line Input #1, str
    str1 = LTrim(str)
    If Left(str1, 31) = "Detailed Review Report of Joint" Then
            strComnJt = Trim(Mid(str1, 35, 4))
            strOutPut = "Common_Jt" & Space(1) & strComnJt
            Print #2, strOutPut

            Line Input #1, strDiscardThisLine
            Line Input #1, str3rdLine
            strOutPut = Trim(Mid(str3rdLine,35,4))
            Print #2, strOutPut

    End If