利用VBA批量导入TXT或LOG文件,并完成筛选汇总(适用于UNIX系统出来的数据)

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

输出结果:

利用VBA批量导入TXT或LOG文件,并完成筛选汇总(适用于UNIX系统出来的数据)_第1张图片

你可能感兴趣的:(VBA)