使用VBAparsing.txt文件中的数据,数据和variables在不同的行上

我有一个.txt文件,改变它显示在某些variables的格式

我能够使用此代码成功提取时间数据:

档案资料:

+---------------------+------------------------------------------------------------------+ | Start Time | 09/06/2015 02:28:58 | | Finish Time | 09/06/2015 03:12:33 | +---------------------+------------------------------------------------------------------+ 

码:

 'open .txt files from defined folder(s) Set fso = CreateObject("Scripting.FileSystemObject") For Each f In fso.GetFolder("C:\Localdata\XYZ").Files If LCase(fso.GetExtensionName(f.Name)) = "txt" Then 'define Row Row = Row + 1 'open each file as a text stream and read until end of file Set stream = f.OpenAsTextStream Do While Not stream.AtEndOfStream Line = stream.ReadLine 'extract the data associated with the text for items in data line If InStr(1, Line, "Start Time") Then 'Add each value to the next available row in designated column (A,B...) Range("A60000").End(xlUp).Offset(1, 0).Value = Trim(Split(Line, "|")(2)) End If 

但是,现在我试图以这种方式parsing出现在文件中的数据:

 | | Variable | | +-----------+-----------+--------+-----------+ | Object | Value1 | Value2 | Value3 | Value4 | | | | | | | +=================================+===========+===========+========+===========+ | Type | 5.00 | 10.00 | 15.00| 20.00 | +---------------------------------+-----------+-----------+--------+-----------+ 

我想标记文本“variables”,并将值拉下Value2(10.00)。 在文本文件中,我有几个像这样的“表格”,它们的可变间距和字符长度

我怎样才能从感兴趣的文本string中跳过几行呢?

我有朋友帮我解决了这个问题,我们发现提取我想要的确切数据的唯一方法是将唯一文本键入,然后closures定义表的+字符:

 If InStr(1, LineTxt, "Variable", vbTextCompare) Then ' Look For unique text in desired table - this starts the next search for "Object" a = 0 'Use this as a switch for breaking out of next DO LOOP Do While a <> 1 'Do this unti a = 1 Line Input #1, LineTxt ' Read line from If InStr(1, LineTxt, "+==========") Then ' Look for Type - This only occurs after "Object" is found, so it will not happen other time "Type" occurs in the log. Line Input #1, LineTxt ' read a line on each loop Row = Row + 1 ' Increment counter for row Worksheets("Output").Range("J60000").End(xlUp).Offset(1, 0).Value = Trim(Split(LineTxt, "|")(3)) 'Once "Type" is found, split the line and write the data to a cell on worksheet "Output" a = 1 ' Set swith to 1 - The Do / While loop will break due to this switch change. End If Loop ' Loop until a = 1 

万一

感谢所有提供build议的人。 我在这个论坛上学到了很多东西!