例子:提取指定区间内容
Private Sub OptionButton7_Click()
Dim Fs As Object, Ft As Object
Set Fs = CreateObject("Scripting.FileSystemObject")
filePath = ThisWorkbook.Path & "\"
Filename = Dir(filePath & "*.log", vbNormal)
Worksheets("VO_F").Cells.Clear
Worksheets("VO_F").Activate
'
Worksheets("VO_F").Range("A1:E1") = Array("ENBIP", "MO", "KEYID", "LICENSE", "FEATURESTATE")
N = 1
Do While Filename <> ""
Set Fs = CreateObject("Scripting.FileSystemObject")
Set Ft = Fs.opentextfile(filePath & Filename)
Do
TEXTLINE = Ft.ReadLinE
If InStr(1, TEXTLINE, "SystemFunctions=1,Lm=1,FeatureState=") > 0 Then
N = N + 1
StrN = Split(WorksheetFunction.Trim(TEXTLINE), " ") 'MO
Worksheets("VO_F").Cells(N, 1) = Filename
Worksheets("VO_F").Cells(N, 2) = StrN(1)
Do
TEXTLINE = Ft.ReadLinE
If InStr(1, TEXTLINE, "featureState ") > 0 Then
StrN1 = Split(WorksheetFunction.Trim(TEXTLINE), " ") 'FEATURESTATE
Worksheets("VO_F").Cells(N, 5) = StrN1(1)
ElseIf InStr(1, TEXTLINE, "keyId ") > 0 Then
StrN2 = Split(WorksheetFunction.Trim(TEXTLINE), " ") 'KEYID
Worksheets("VO_F").Cells(N, 3) = StrN2(1)
ElseIf InStr(1, TEXTLINE, "licenseState ") > 0 Then
StrN3 = Split(WorksheetFunction.Trim(TEXTLINE), " ") 'LICENSE
Worksheets("VO_F").Cells(N, 4) = StrN3(1)
Count = UBound(StrN)
End If
Loop Until InStr(1, TEXTLINE, "Total:") > 0
End If
Loop Until Ft.atendofstream 'Ft.atendofline 'Ft.AtEndOfLine
Set Fs = Nothing
Set Ft = Nothing
Close #1
Filename = Dir
Loop
MsgBox ("已完筛选、合并操作!")
End Sub
例子2:提取指定内容
Sub 批量导入TXT或LOG并完成汇总()
Dim Fs As Object, Ft As Object
filePath = ThisWorkbook.Path & "\"
fileName = Dir(filePath & "*.log", vbNormal)
Worksheets("SHEET1").Cells.Clear
Worksheets("SHEET1").Range("A1:D1") = Array("ENBIP", "MO", "参数", "值")
N = 1
Do While fileName <> ""
Set Fs = CreateObject("Scripting.FileSystemObject")
Set Ft = Fs.opentextfile(filePath & fileName)
Do
TEXTLINE = Ft.ReadLinE
If InStr(1, TEXTLINE, "EUtranCell") > 0 Then
N = N + 1
strn = Split(TEXTLINE, " ")
Count = UBound(strn)
If Count = 2 Then
Worksheets("SHEET1").Cells(N, 1) = fileName
Worksheets("SHEET1").Cells(N, 2) = strn(0)
Worksheets("SHEET1").Cells(N, 3) = strn(1)
Worksheets("SHEET1").Cells(N, 4) = strn(2)
ElseIf Count = 3 Then
Worksheets("SHEET1").Cells(N, 1) = fileName
Worksheets("SHEET1").Cells(N, 2) = strn(0)
Worksheets("SHEET1").Cells(N, 3) = strn(1)
Worksheets("SHEET1").Cells(N, 4) = strn(3)
End If
End If
Loop Until Ft.atendofstream 'Ft.atendofline 'Ft.AtEndOfLine
Set Fs = Nothing
Set Ft = Nothing
Close #1
fileName = Dir
Loop
MsgBox ("已完筛选、合并操作!")
End Sub
输出结果: