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

例子:提取指定区间内容


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

输出结果:

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

qq_44390640

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值