VBA宏:捕获具有特定字符串的行并返回它
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了VBA宏:捕获具有特定字符串的行并返回它相关的知识,希望对你有一定的参考价值。
我试图从模拟报告(.log)中捕获模拟结果。我想匹配“传递”和“失败”字符串并保存在其他结果文件中完成匹配的行,我将用于进一步操作Excel中。我无法匹配所需的行:我写的下面的子不仅写了所需的行而且写了所有的行。我不知道问题在哪里..
Sub generate_sim_results()
Dim testResults As String
Dim InputFile As String
Dim fso As Object
Dim ObjFile As Object
Set fso = CreateObject("Scripting.FileSystemObject")
testResults = Application.ActiveWorkbook.Path & "sim_results.log"
If Len(Dir$(testResults)) > 0 Then
Kill testResults
End If
InputFile = Dir(Application.ActiveWorkbook.Path & "" & "*.log")
InputFile = Application.ActiveWorkbook.Path & "" & InputFile
'Debug.Print InputFile
fnum = FreeFile()
Open InputFile For Input As #fnum
Set ObjFile = fso.CreateTextFile(testResults)
While Not EOF(fnum)
Line Input #fnum, dataLine
If InStr(1, dataLine, "passed", vbTextCompare) > 0 Or InStr(1, dataLine,"failed", vbTextCompare) > 0 Then
ObjFile.Writeline dataLine
Debug.Print dataLine
End If
Wend
ObjFile.Close
Set fso = Nothing
Set ObjFile = Nothing
End Sub
*sample of the log file*
<time="1500 ns" instance="testcase_00">passed
<time="342100480 ps" instance="testcase_01">passed
blabla informations about the tests....
<time="742894 ns" instance="testcase_02_01">passed
blabla informations about the tests....
blabla informations about the tests....
<time="744121040 ps" instance="testcase_02_02">failed
blabla informations about the tests....
<time="745034560 ps" instance="testcase_02_03">passed
blabla informations about the tests....
<time="745134560 ps" instance="testcase_02_04">passed
blabla informations about the tests....
blabla informations about the tests....
blabla informations about the tests....
blabla informations about the tests....
<time="745548080 ps" instance="testcase_03">failed
<time="747388640 ps" instance="testcase_04_01">passed
<time="750745100 ns" instance="testcase_04_02">passed
blabla informations about the tests....
想只返回带有传递和失败字符串的行,并将其写入另一个日志文件(sim_results)
答案
当使用Line Input读取文件时,您必须考虑到线路输入仅在读取CR或CR和LF时停止,但在它仅读取LF时不会停止。在您的情况下,您需要将LF转换为CR和LF。你可以用Notpad ++做到这一点
由于您已经使用FSO,您也可以使用它来读取这样的文件。那你就不用担心“Unix”了。
Sub generate_sim_results()
Dim testResults As String
Dim InputFile As String
Dim fso As Object
Dim ObjFile As Object
Dim fnum As Long
Dim dataLine As String
Set fso = CreateObject("Scripting.FileSystemObject")
testResults = Application.ActiveWorkbook.Path & "sim_results.log"
If Len(Dir$(testResults)) > 0 Then
Kill testResults
End If
InputFile = Dir(Application.ActiveWorkbook.Path & "" & "*.log")
InputFile = Application.ActiveWorkbook.Path & "" & InputFile
'Debug.Print InputFile
fnum = FreeFile()
Open InputFile For Input As #fnum
Set ObjFile = fso.CreateTextFile(testResults)
Dim myFile
Set myFile = fso.OpenTextFile(InputFile, 1)
Do While myFile.AtEndOfStream <> True
dataLine = myFile.ReadLine
If InStr(1, dataLine, "passed", vbTextCompare) > 0 Or InStr(1, dataLine, "failed", vbTextCompare) > 0 Then
ObjFile.Writeline dataLine
Debug.Print dataLine
End If
Loop
myFile.Close
ObjFile.Close
Set fso = Nothing
Set ObjFile = Nothing
End Sub
以上是关于VBA宏:捕获具有特定字符串的行并返回它的主要内容,如果未能解决你的问题,请参考以下文章