从文件中找到字符串后提取某些行

时间:2019-06-25 10:38:45

标签: excel vba

我想在文件的某些行中找到字符串后提取第6、9和12行。这里的搜索字符串是输出名称=“ BoundingBox”> 。同一文件中可能有多个实例,因此我想重复相同的内容。

我下面的代码在找到字符串后提取所有行。

文件数据:

  ...
  <output name="BoundingBox">
   <struct type="BoundingBox">
  <output name="BottomLeftCorner">
   <struct type="BottomLeftCorner">
  <elem name="X">
    <object type="double" value = "351.8340105482794">
  </elem>
  <elem name="Y">
    <object type="double" value = "319.5377197265625">
  </elem>
  <elem name="Z">
    <object type="double" value = "0">
  </elem>
   </struct>
  </output>
  <output name="TopRightCorner">
   <struct type="TopRightCorner">

代码:

Sub test()
Dim fn As String, txt As String, mySearch As String, x, y
fn = "D:\workdir\Autotest--new.xml"
txt = CreateObject("Scripting.FileSystemObject").OpenTextFile(fn).ReadAll
'mySearch = InputBox("Enter search string")
x = InStr(1, txt, "output name=""BoundingBox"">", 1)
If x = 0 Then
    MsgBox "No match"
    Exit Sub
Else
    txt = Mid$(txt, x)
    y = Split(txt, vbCrLf)
    Cells(1, 1).Resize(UBound(y) + 1).Value = Application.Transpose(y)
End If
End Sub

1 个答案:

答案 0 :(得分:1)

我将使用Open fn For Input As #1并使用Line Input #1, textline遍历各行,然后检查String是否存在并计数以下各行。保存所需的一切。不要忘记使用Close #1

关闭文件
Sub test()
    Dim fn As String, txt As String, mySearch As String, x, y
    fn = "D:\workdir\Autotest--new.xml"
    Open fn For Input As #1
    Do Until EOF(1)
        Line Input #1, textline
        If InStr(1, textline, "output name=""BoundingBox"">", 1) > 0 Then
            For i = 1 To 12
                Line Input #1, textline
                Select Case i
                    Case 5
                        Cells(1, 1) = extract_value(textline)
                    Case 8
                        Cells(1, 2) = extract_value(textline)
                    Case 11
                        Cells(1, 3) = extract_value(textline)
                    Case Else
                        'Ignore
                End Select
            Next
        End If
    Loop
    Close #1
End Sub

下面的函数从文本行中提取双精度值。

Function extract_value(ByVal textline As String) As Double
    startIndex = InStr(1, textline, "value = """) + 9
    endIndex = InStr(startIndex, textline, """")
    strVal = Mid(textline, startIndex, endIndex - startIndex)
    'strVal = Replace(strVal, ".", ",") 'use this if you have "," as your decimal separator
    extract_value = CDbl(strVal)
End Function

此代码未经测试,因为我没有您的实例的完整文件。

如果您要处理XML文件,我建议进一步看一下XML解析器:http://dailydoseofexcel.com/archives/2009/06/16/reading-xml-files-in-vba/