Excel 使用VBA、数据和变量在不同的行上解析.txt文件中的数据
我有一个.txt文件,它改变了某些变量的显示格式 我能够使用以下代码成功提取时间数据: 文件数据:Excel 使用VBA、数据和变量在不同的行上解析.txt文件中的数据,excel,vba,parsing,text,Excel,Vba,Parsing,Text,我有一个.txt文件,它改变了某些变量的显示格式 我能够使用以下代码成功提取时间数据: 文件数据: +---------------------+------------------------------------------------------------------+ | Start Time | 09/06/2015 02:28:58 | | Finish Time | 09
+---------------------+------------------------------------------------------------------+
| Start Time | 09/06/2015 02:28:58 |
| Finish Time | 09/06/2015 03:12:33 |
+---------------------+------------------------------------------------------------------+
代码:
但是,现在我尝试以以下方式解析文件中显示的数据:
| | Variable |
| +-----------+-----------+--------+-----------+
| Object | Value1 | Value2 | Value3 | Value4 |
| | | | | |
+=================================+===========+===========+========+===========+
| Type | 5.00 | 10.00 | 15.00| 20.00 |
+---------------------------------+-----------+-----------+--------+-----------+
我想标记掉文本“Variable”并提取Value2(10.00)下的值。
我在文本文件中有几个这样的“表格”,间距和字符长度可变
如何让它从感兴趣的文本字符串中向下跳几行?我让朋友帮我解决了这个问题,我们发现提取我想要的确切数据的唯一方法是键入唯一文本,然后去掉定义表的+---字符:
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
如果InStr(1,LineTxt,“Variable”,vbTextCompare),则“在所需表格中查找唯一文本-这将开始下一次搜索“Object”
a=0'将其用作断开下一个DO循环的开关
当a为1'时执行此操作,直到a=1
行输入#1,LineTxt'从读取行
如果InStr(1,LineTxt,“+===================”),则“查找类型-这仅在找到“对象”后发生,因此在日志中出现“类型”时不会发生。
行输入#1,LineTxt'读取每个循环上的行
行=行+1'行的增量计数器
工作表(“输出”).Range(“J60000”).End(xlUp).Offset(1,0).Value=Trim(Split(LineTxt,“|”)(3))”找到“Type”后,拆分该行并将数据写入工作表“Output”上的单元格
a=1'将开关设置为1-Do/While循环将因开关更改而中断。
如果结束
“循环”循环直到a=1
如果结束
感谢所有提供建议的人。在这个论坛上我学到了很多 首先想到的想法是使用一个变量来存储读取行时的模式,比如说
如果InStr(1,行,“variable”,vbTextCompare)>0,那么bLookForValue=True
,那么从下一行开始,对拆分|
中的项目使用不同的测试用例,并使用Len(…)>0
,是数字(…)
来提取值,检索到值时,bLookForValue=False
。删除每行的空格也可能有帮助。“Value2”是否始终位于同一位置,或者在从下一行提取值之前是否需要先找到它?谢谢您的帮助。很乱。在本例中,Value2将始终位于该位置,并且可能在文件中重复多次。但是还有一些我感兴趣的数据表。在这些情况下,值2可能位于行中的不同行或位置。更糟糕的是,有些表没有唯一的列标题,因此可能有多个“值”字符串。这就是为什么我需要以某种方式关闭文本的“变量”。在数字上,数据值也不总是唯一的。蒂姆:谢谢你在我的帖子中修复了表格显示。你是怎么做到的?我使用了代码示例{}
按钮。
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