其他分享
首页 > 其他分享> > 利用VBA批量导入TXT或LOG文件,并完成筛选汇总

利用VBA批量导入TXT或LOG文件,并完成筛选汇总

作者:互联网

Sub 批量导入TXT或LOG并完成汇总()
    Dim Fs As Object, Ft As Object
    Set Fs = CreateObject("Scripting.FileSystemObject")
    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,Ft,LOG,Cells,fileName,SHEET1,strn,Worksheets,TXT
来源: https://blog.csdn.net/qq_44390640/article/details/102719368