Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/database/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Excel 基于行ID循环行并将值复制到列中的VBA代码_Excel_Vba - Fatal编程技术网

Excel 基于行ID循环行并将值复制到列中的VBA代码

Excel 基于行ID循环行并将值复制到列中的VBA代码,excel,vba,Excel,Vba,我有来自测量设备的原始现场数据文本文件,我希望在新工作表的汇总表中进行安排;我创建了一个代码,通过将每个文件追加到一个表中来导入多个文本文件,但是原始数据文件包含了太多摘要中不需要的行。在每个文本文件中输出的大约1000行中,汇总表只需要16行。数据是根据每个测试编号排列的,因此“第2列”中的“rowid”与报告的指标相同。 我想帮助创建宏以循环通过原始数据表,基于“第1列”作为“TestID”和“第2列”作为“ROWID”,然后将“第4列”中的值复制到汇总表中,下面的示例,欢迎提供任何帮助或问

我有来自测量设备的原始现场数据文本文件,我希望在新工作表的汇总表中进行安排;我创建了一个代码,通过将每个文件追加到一个表中来导入多个文本文件,但是原始数据文件包含了太多摘要中不需要的行。在每个文本文件中输出的大约1000行中,汇总表只需要16行。数据是根据每个测试编号排列的,因此“第2列”中的“rowid”与报告的指标相同。 我想帮助创建宏以循环通过原始数据表,基于“第1列”作为“TestID”和“第2列”作为“ROWID”,然后将“第4列”中的值复制到汇总表中,下面的示例,欢迎提供任何帮助或问题。多谢各位

样本数据如下

Ontario2    5100    FILE NAME & SYSTEM MODEL              : Ontario2.sks  1295 PAVEMENT FRICTION TESTER (UNIVERSAL)     
Ontario2    5101    SOFTWARE VERSION                      : 1.0.6506.22298      
Ontario2    5101A   VCU SOFTWARE VERSION                 : 8000908.601.007a     
Ontario2    5102    TIME                       (hh:mm:ss) : 11:44:13        
Ontario2    5103    DATE                     (mm/dd/yyyy) : 01/22/2019      
Ontario2    0   =================================================================       
Ontario2    0    Test Summary Values                 Value           Units
Ontario2    0   -----------------------------------------------------------------       
Ontario2    5000     Test Number                        1    
Ontario2    5001     Route                                               
Ontario2    5002     Direction                                           
Ontario2    5003     Lane                                                
Ontario2    5004     Event                                               
Ontario2    5005     Test Start DMI                     0    ft
Ontario2    5006     Test Start Time (Hr)               11   hr
Ontario2    5007     Test Start Time (Min)              40   minute
Ontario2    5008     Test Wheel                          Left            
Ontario2    5009     Test Wet/Dry                        WET             
Ontario2    5010     Latitude                            034.0564687° N  
Ontario2    5011     Longitude                           084.3002236° W  
Ontario2    5012     Bearing                            179.9    degrees
Ontario2    6000     Air Temperature                    -3.5     f
Ontario2    6001     Surface Temperature                55.6     f
Ontario2    6002     Left Tire Temperature               not installed   f
Ontario2    6003     Left Nozzle Water Temperature      0    f
Ontario2    6004     Right Tire Temperature              not installed   f
Ontario2    6005     Right Nozzle Water Temperature      not installed   f
Ontario2    6006     Tank 1 Water Temperature           0    f
Ontario2    6007     Tank 2 Water Temperature            not installed   f
Ontario2    6008     Tank 1 Water Level                 325.9    gal
Ontario2    6009     Tank 2 Water Level                  not installed   gal
Ontario2    6010     Data Filter                        5    points
Ontario2    6011     Water Depth                        0.5  mm
Ontario2    6012     Sampling Rate                      100  hz
Ontario2    0   -----------------------------------------------------------------       
Ontario2    0    Right Wheel Peak Values             Value           Units
Ontario2    0   -----------------------------------------------------------------       
Ontario2    6020     Right Traction Force               0    lb
Ontario2    6021     Right Load Force                   0    lb
Ontario2    6022     Right Vehicle Speed                0    mph
Ontario2    6023     Right Test Wheel Speed             0    mph
Ontario2    6024     Right SN                           0    friction
Ontario2    6025     Right Percent Slip                 0    percent
Ontario2    6026     Right Time to Peak Brake Coef      0    seconds
Ontario2    6027     Right Time to Wheel Lockup         0    seconds
Ontario2    6028     Right Flow Rate                    0    gpm
Ontario2    0   -----------------------------------------------------------------       
Ontario2    0    Left Wheel Peak Values              Value           Units
Ontario2    0   -----------------------------------------------------------------       
Ontario2    6040     Left  Traction Force               1029.24  lb
Ontario2    6041     Left  Load Force                   949.52   lb
Ontario2    6042     Left  Vehicle Speed                37.5     mph
Ontario2    6043     Left  Test Wheel Speed             33.1     mph
Ontario2    6044     Left  SN                           108.4    friction
Ontario2    6045     Left  Percent Slip                 11.6     percent
Ontario2    6046     Left  Time to Peak Brake Coef      1.13     seconds
Ontario2    6047     Left  Time to Wheel Lockup         1.37     seconds
Ontario2    6048     Left  Flow Rate                    0    gpm
Ontario2    0   -----------------------------------------------------------------       
Ontario2    0    Right Wheel SN Values               Value           Units
Ontario2    0   -----------------------------------------------------------------       
Ontario2    6060     Right SN Average                   0    friction
Ontario2    6061     Right SN Minimum                   0    friction
Ontario2    6062     Right SN Maximum                   0    friction
Ontario2    6063     Right SN Standard Deviation        0    friction
Ontario2    6064     Right SN Speed Average             0    mph
Ontario2    6065     Right SN Flow Average              0    gpm
Ontario2    0   -----------------------------------------------------------------       
Ontario2    0    Left Wheel SN Values                Value           Units
Ontario2    0   -----------------------------------------------------------------       
Ontario2    6080     Left  SN Average                   84.2     friction
Ontario2    6081     Left  SN Minimum                   78.3     friction
Ontario2    6082     Left  SN Maximum                   89.4     friction
Ontario2    6083     Left  SN Standard Deviation        2.7  friction
Ontario2    6084     Left  SN Speed Average             36.6     mph
Ontario2    6085     Left  SN Flow Average              0    gpm
Ontario2    0   =================================================================       
Ontario2    XXXX    ^^^^ Speed ^^^^     
Ontario2    0   =================================================================       
Ontario2    0    Test Summary Values                 Value           Units
Ontario2    0   -----------------------------------------------------------------       
Ontario2    5000     Test Number                        2    
Ontario2    5001     Route                                               
Ontario2    5002     Direction                                           
Ontario2    5003     Lane                                                
Ontario2    5004     Event                                               
Ontario2    5005     Test Start DMI                     29.36    ft
Ontario2    5006     Test Start Time (Hr)               11   hr
Ontario2    5007     Test Start Time (Min)              41   minute
Ontario2    5008     Test Wheel                          Left            
Ontario2    5009     Test Wet/Dry                        WET             
Ontario2    5010     Latitude                            034.0511896° N  
Ontario2    5011     Longitude                           084.3033369° W  
Ontario2    5012     Bearing                            201.9    degrees
Ontario2    6000     Air Temperature                    -1.6     f
Ontario2    6001     Surface Temperature                49.9     f
Ontario2    6002     Left Tire Temperature               not installed   f
Ontario2    6003     Left Nozzle Water Temperature      0    f
Ontario2    6004     Right Tire Temperature              not installed   f
Ontario2    6005     Right Nozzle Water Temperature      not installed   f
Ontario2    6006     Tank 1 Water Temperature           0    f
Ontario2    6007     Tank 2 Water Temperature            not installed   f
Ontario2    6008     Tank 1 Water Level                 353.3    gal
Ontario2    6009     Tank 2 Water Level                  not installed   gal
Ontario2    6010     Data Filter                        5    points
Ontario2    6011     Water Depth                        0.5  mm
Ontario2    6012     Sampling Rate                      100  hz
Ontario2    0   -----------------------------------------------------------------       
Ontario2    0    Right Wheel Peak Values             Value           Units
Ontario2    0   -----------------------------------------------------------------       
Ontario2    6020     Right Traction Force               0    lb
Ontario2    6021     Right Load Force                   0    lb
Ontario2    6022     Right Vehicle Speed                0    mph
Ontario2    6023     Right Test Wheel Speed             0    mph
Ontario2    6024     Right SN                           0    friction
Ontario2    6025     Right Percent Slip                 0    percent
Ontario2    6026     Right Time to Peak Brake Coef      0    seconds
Ontario2    6027     Right Time to Wheel Lockup         0    seconds
Ontario2    6028     Right Flow Rate                    0    gpm
Ontario2    0   -----------------------------------------------------------------       
Ontario2    0    Left Wheel Peak Values              Value           Units
Ontario2    0   -----------------------------------------------------------------       
Ontario2    6040     Left  Traction Force               880.6    lb
Ontario2    6041     Left  Load Force                   959.17   lb
Ontario2    6042     Left  Vehicle Speed                40.5     mph
Ontario2    6043     Left  Test Wheel Speed             36   mph
Ontario2    6044     Left  SN                           91.81    friction
Ontario2    6045     Left  Percent Slip                 11.2     percent
Ontario2    6046     Left  Time to Peak Brake Coef      0.62     seconds
Ontario2    6047     Left  Time to Wheel Lockup         0.76     seconds
Ontario2    6048     Left  Flow Rate                    29.1     gpm
Ontario2    0   -----------------------------------------------------------------       
Ontario2    0    Right Wheel SN Values               Value           Units
Ontario2    0   -----------------------------------------------------------------       
Ontario2    6060     Right SN Average                   0    friction
Ontario2    6061     Right SN Minimum                   0    friction
Ontario2    6062     Right SN Maximum                   0    friction
Ontario2    6063     Right SN Standard Deviation        0    friction
Ontario2    6064     Right SN Speed Average             0    mph
Ontario2    6065     Right SN Flow Average              0    gpm
Ontario2    0   -----------------------------------------------------------------       
Ontario2    0    Left Wheel SN Values                Value           Units
Ontario2    0   -----------------------------------------------------------------       
Ontario2    6080     Left  SN Average                   53.4     friction
Ontario2    6081     Left  SN Minimum                   51.5     friction
Ontario2    6082     Left  SN Maximum                   55.5     friction
Ontario2    6083     Left  SN Standard Deviation        0.9  friction
Ontario2    6084     Left  SN Speed Average             40   mph
Ontario2    6085     Left  SN Flow Average              28.8     gpm
Ontario2    0   =================================================================       
Ontario2    0   =================================================================       
Ontario2    0    Test Summary Values                 Value           Units
Ontario2    0   -----------------------------------------------------------------       

Ontario2    5000     Test Number                        12   
Ontario2    5001     Route                                               
Ontario2    5002     Direction                                           
Ontario2    5003     Lane                                                
Ontario2    5004     Event                                               
Ontario2    5005     Test Start DMI                     8053.43  ft
Ontario2    5006     Test Start Time (Hr)               11   hr
Ontario2    5007     Test Start Time (Min)              44   minute
Ontario2    5008     Test Wheel                          Left            
Ontario2    5009     Test Wet/Dry                        WET             
Ontario2    5010     Latitude                            034.0483463° N  
Ontario2    5011     Longitude                           084.3183640° W  
Ontario2    5012     Bearing                            41.7     degrees
Ontario2    6000     Air Temperature                    -1.6     f
Ontario2    6001     Surface Temperature                47.6     f
Ontario2    6002     Left Tire Temperature               not installed   f
Ontario2    6003     Left Nozzle Water Temperature      0    f
Ontario2    6004     Right Tire Temperature              not installed   f
Ontario2    6005     Right Nozzle Water Temperature      not installed   f
Ontario2    6006     Tank 1 Water Temperature           0    f
Ontario2    6007     Tank 2 Water Temperature            not installed   f
Ontario2    6008     Tank 1 Water Level                 326  gal
Ontario2    6009     Tank 2 Water Level                  not installed   gal
Ontario2    6010     Data Filter                        5    points
Ontario2    6011     Water Depth                        0.5  mm
Ontario2    6012     Sampling Rate                      100  hz
Ontario2    0   -----------------------------------------------------------------       
Ontario2    0    Right Wheel Peak Values             Value           Units
Ontario2    0   -----------------------------------------------------------------       
Ontario2    6020     Right Traction Force               0    lb
Ontario2    6021     Right Load Force                   0    lb
Ontario2    6022     Right Vehicle Speed                0    mph
Ontario2    6023     Right Test Wheel Speed             0    mph
Ontario2    6024     Right SN                           0    friction
Ontario2    6025     Right Percent Slip                 0    percent
Ontario2    6026     Right Time to Peak Brake Coef      0    seconds
Ontario2    6027     Right Time to Wheel Lockup         0    seconds
Ontario2    6028     Right Flow Rate                    0    gpm
Ontario2    0   -----------------------------------------------------------------       
Ontario2    0    Left Wheel Peak Values              Value           Units
Ontario2    0   -----------------------------------------------------------------       
Ontario2    6040     Left  Traction Force               825.19   lb
Ontario2    6041     Left  Load Force                   946.42   lb
Ontario2    6042     Left  Vehicle Speed                40.6     mph
Ontario2    6043     Left  Test Wheel Speed             37.2     mph
Ontario2    6044     Left  SN                           87.19    friction
Ontario2    6045     Left  Percent Slip                 8.6  percent
Ontario2    6046     Left  Time to Peak Brake Coef      0.59     seconds
Ontario2    6047     Left  Time to Wheel Lockup         0.71     seconds
Ontario2    6048     Left  Flow Rate                    29.3     gpm
Ontario2    0   -----------------------------------------------------------------       
Ontario2    0    Right Wheel SN Values               Value           Units
Ontario2    0   -----------------------------------------------------------------       
Ontario2    6060     Right SN Average                   0    friction
Ontario2    6061     Right SN Minimum                   0    friction
Ontario2    6062     Right SN Maximum                   0    friction
Ontario2    6063     Right SN Standard Deviation        0    friction
Ontario2    6064     Right SN Speed Average             0    mph
Ontario2    6065     Right SN Flow Average              0    gpm
Ontario2    0   -----------------------------------------------------------------       
Ontario2    0    Left Wheel SN Values                Value           Units
Ontario2    0   -----------------------------------------------------------------       
Ontario2    6080     Left  SN Average                   51.5     friction
Ontario2    6081     Left  SN Minimum                   47   friction
Ontario2    6082     Left  SN Maximum                   54.8     friction
Ontario2    6083     Left  SN Standard Deviation        1.8  friction
Ontario2    6084     Left  SN Speed Average             40.2     mph
Ontario2    6085     Left  SN Flow Average              28.9     gpm
Ontario2    0   =================================================================       
Ontario2    0   =================================================================       
Ontario3 - Copy (2) 5100    FILE NAME & SYSTEM MODEL              : Ontario3.sks  1295 PAVEMENT FRICTION TESTER (UNIVERSAL)     
Ontario3 - Copy (2) 5101    SOFTWARE VERSION                      : 1.0.6506.22298      
Ontario3 - Copy (2) 5101A   VCU SOFTWARE VERSION                 : 8000908.601.007a     
Ontario3 - Copy (2) 5102    TIME                       (hh:mm:ss) : 11:44:13        
Ontario3 - Copy (2) 5103    DATE                     (mm/dd/yyyy) : 01/22/2019      
Ontario3 - Copy (2) 0   =================================================================       
Ontario3 - Copy (2) 0    Test Summary Values                 Value           Units
Ontario3 - Copy (2) 0   -----------------------------------------------------------------       
Ontario3 - Copy (2) 5000     Test Number                        1    
Ontario3 - Copy (2) 5001     Route                                               
Ontario3 - Copy (2) 5002     Direction                                           
Ontario3 - Copy (2) 5003     Lane                                                
Ontario3 - Copy (2) 5004     Event                                               
Ontario3 - Copy (2) 5005     Test Start DMI                     0    ft
Ontario3 - Copy (2) 5006     Test Start Time (Hr)               11   hr
Ontario3 - Copy (2) 5007     Test Start Time (Min)              40   minute
Ontario3 - Copy (2) 5008     Test Wheel                          Left            
Ontario3 - Copy (2) 5009     Test Wet/Dry                        WET             
Ontario3 - Copy (2) 5010     Latitude                            034.0564687° N  
Ontario3 - Copy (2) 5011     Longitude                           084.3002236° W  
Ontario3 - Copy (2) 5012     Bearing                            179.9    degrees
Ontario3 - Copy (2) 6000     Air Temperature                    -3.5     f
Ontario3 - Copy (2) 6001     Surface Temperature                55.6     f
Ontario3 - Copy (2) 6002     Left Tire Temperature               not installed   f
Ontario3 - Copy (2) 6003     Left Nozzle Water Temperature      0    f
Ontario3 - Copy (2) 6004     Right Tire Temperature              not installed   f
Ontario3 - Copy (2) 6005     Right Nozzle Water Temperature      not installed   f
Ontario3 - Copy (2) 6006     Tank 1 Water Temperature           0    f
Ontario3 - Copy (2) 6007     Tank 2 Water Temperature            not installed   f
Ontario3 - Copy (2) 6008     Tank 1 Water Level                 325.9    gal
Ontario3 - Copy (2) 6009     Tank 2 Water Level                  not installed   gal
Ontario3 - Copy (2) 6010     Data Filter                        5    points
Ontario3 - Copy (2) 6011     Water Depth                        0.5  mm
Ontario3 - Copy (2) 6012     Sampling Rate                      100  hz
Ontario3 - Copy (2) 0   -----------------------------------------------------------------       
Ontario3 - Copy (2) 0    Right Wheel Peak Values             Value           Units
Ontario3 - Copy (2) 0   -----------------------------------------------------------------       
Ontario3 - Copy (2) 6020     Right Traction Force               0    lb
Ontario3 - Copy (2) 6021     Right Load Force                   0    lb
Ontario3 - Copy (2) 6022     Right Vehicle Speed                0    mph
Ontario3 - Copy (2) 6023     Right Test Wheel Speed             0    mph
Ontario3 - Copy (2) 6024     Right SN                           0    friction
Ontario3 - Copy (2) 6025     Right Percent Slip                 0    percent
Ontario3 - Copy (2) 6026     Right Time to Peak Brake Coef      0    seconds
Ontario3 - Copy (2) 6027     Right Time to Wheel Lockup         0    seconds
Ontario3 - Copy (2) 6028     Right Flow Rate                    0    gpm
Ontario3 - Copy (2) 0   -----------------------------------------------------------------       
Ontario3 - Copy (2) 0    Left Wheel Peak Values              Value           Units
Ontario3 - Copy (2) 0   -----------------------------------------------------------------       
Ontario3 - Copy (2) 6040     Left  Traction Force               1029.24  lb
Ontario3 - Copy (2) 6041     Left  Load Force                   949.52   lb
Ontario3 - Copy (2) 6042     Left  Vehicle Speed                37.5     mph
Ontario3 - Copy (2) 6043     Left  Test Wheel Speed             33.1     mph
Ontario3 - Copy (2) 6044     Left  SN                           108.4    friction
Ontario3 - Copy (2) 6045     Left  Percent Slip                 11.6     percent
Ontario3 - Copy (2) 6046     Left  Time to Peak Brake Coef      1.13     seconds
Ontario3 - Copy (2) 6047     Left  Time to Wheel Lockup         1.37     seconds
Ontario3 - Copy (2) 6048     Left  Flow Rate                    0    gpm
Ontario3 - Copy (2) 0   -----------------------------------------------------------------       
Ontario3 - Copy (2) 0    Right Wheel SN Values               Value           Units
Ontario3 - Copy (2) 0   -----------------------------------------------------------------       
Ontario3 - Copy (2) 6060     Right SN Average                   0    friction
Ontario3 - Copy (2) 6061     Right SN Minimum                   0    friction
Ontario3 - Copy (2) 6062     Right SN Maximum                   0    friction
Ontario3 - Copy (2) 6063     Right SN Standard Deviation        0    friction
Ontario3 - Copy (2) 6064     Right SN Speed Average             0    mph
Ontario3 - Copy (2) 6065     Right SN Flow Average              0    gpm
Ontario3 - Copy (2) 0   -----------------------------------------------------------------       
Ontario3 - Copy (2) 0    Left Wheel SN Values                Value           Units
Ontario3 - Copy (2) 0   -----------------------------------------------------------------       
Ontario3 - Copy (2) 6080     Left  SN Average                   84.2     friction
Ontario3 - Copy (2) 6081     Left  SN Minimum                   78.3     friction
Ontario3 - Copy (2) 6082     Left  SN Maximum                   89.4     friction
Ontario3 - Copy (2) 6083     Left  SN Standard Deviation        2.7  friction
Ontario3 - Copy (2) 6084     Left  SN Speed Average             36.6     mph
Ontario3 - Copy (2) 6085     Left  SN Flow Average              0    gpm
Ontario3 - Copy (2) 0   =================================================================       
Ontario3 - Copy (2) XXXX    ^^^^ Speed ^^^^     
Ontario3 - Copy (2) 0   =================================================================       
Ontario3 - Copy (2) 0    Test Summary Values                 Value           Units
Ontario3 - Copy (2) 0   -----------------------------------------------------
预期产出汇总表

Test_ID Test    Distance    Time    Wheel   WetDry  Speed   AvgFN   MinFN   MaxFN   StDevFN Temp    GPS_Lat GPS_Long    Notes   TestDate
Ontario2    1   0   11:40    Left            WET            36.6    84.2    78.3    89.4    2.7 55.6     034.0564687° N  084.3002236° W     1/22/2019
Ontario2    2   29.36   11:41    Left            WET            40  53.4    51.5    55.5    0.9 49.9     034.0511896° N  084.3033369° W     1/22/2019

这里是一个用于此目的的框架代码,可以根据您的需求进行修改。将输入文本文件视为具有多个空格的固定宽度。使用提供的样本数据进行测试。为了简单起见,对输出表结构进行了如下修改

代码直接处理文本文件

Sub test()
Dim Fno As Integer, Ln As String, Arr As Variant
Dim TestNo As String, TestID As String, Wheel As String
Dim Rw As Long
Dim Ws As Worksheet
Dim MyFile As Variant

Set Ws = ThisWorkbook.ActiveSheet

With Application.FileDialog(msoFileDialogFilePicker)
   .AllowMultiSelect = True
   .Filters.Add "Text", "*.Txt", 1

    If .Show <> -1 Then Exit Sub
    Rw = 6

    For Each MyFile In .SelectedItems
    Fno = FreeFile

    Open MyFile For Input As #Fno

    TestID = ""
    TestNo = ""
    Wheel = ""

        Do While Not EOF(Fno)    ' Loop until end of file.
        Line Input #Fno, Ln    ' Read line into variable.
        Ln = Trim(Ln)

        ' multiple spaces are converted to single space for splitting
            Do Until InStr(1, Ln, "  ") = 0
            Ln = Replace(Ln, "  ", " ")
            Arr = Split(Ln, " ")
            Loop

            If UBound(Arr) > 2 Then
                Select Case Arr(1)
                Case "5000"        'TestNo
                    If TestID <> Arr(0) Or TestNo <> Arr(UBound(Arr)) Then
                    Rw = Rw + 1
                    TestID = Arr(0)
                    TestNo = Arr(UBound(Arr))
                    Ws.Cells(Rw, 1).Value = TestID
                    Ws.Cells(Rw, 2).Value = TestNo
                    End If
                Case "5005"   'Distance
                Ws.Cells(Rw, 3).Value = Arr(UBound(Arr) - 1)
                Case "5006"   'Start Hr
                Ws.Cells(Rw, 4).Value = Arr(UBound(Arr) - 1)
                Case "5007"    'Start min
                Ws.Cells(Rw, 5).Value = Arr(UBound(Arr) - 1)
                Case "5008"    'Wheel
                Wheel = Arr(UBound(Arr))
                Ws.Cells(Rw, 6).Value = Arr(UBound(Arr))
                Case "5009"    'WetDry
                Ws.Cells(Rw, 7).Value = Arr(UBound(Arr))
                Case "5010"    'Lat
                Ws.Cells(Rw, 14).Value = Arr(UBound(Arr) - 1) & " " & Arr(UBound(Arr))
                Case "5011"    'Long
                Ws.Cells(Rw, 15).Value = Arr(UBound(Arr) - 1) & " " & Arr(UBound(Arr))
                Case "6001"    'Surface Temp
                Ws.Cells(Rw, 13).Value = Arr(UBound(Arr) - 1)
                End Select

                If Wheel = "Right" Then
                    Select Case Arr(1)
                    Case "6060"          'SN Average
                    Ws.Cells(Rw, 9).Value = Arr(UBound(Arr) - 1)
                    Case "6061"          'SN Min
                    Ws.Cells(Rw, 10).Value = Arr(UBound(Arr) - 1)
                    Case "6062"          'SN MAx
                    Ws.Cells(Rw, 11).Value = Arr(UBound(Arr) - 1)
                    Case "6063"          'SN St Dev
                    Ws.Cells(Rw, 12).Value = Arr(UBound(Arr) - 1)
                    Case "6064"          'Speed Avg
                    Ws.Cells(Rw, 8).Value = Arr(UBound(Arr) - 1)
                    End Select
                Else
                    Select Case Arr(1)
                    Case "6080"          'SN Average
                    Ws.Cells(Rw, 9).Value = Arr(UBound(Arr) - 1)
                    Case "6081"          'SN Min
                    Ws.Cells(Rw, 10).Value = Arr(UBound(Arr) - 1)
                    Case "6082"          'SN MAx
                    Ws.Cells(Rw, 11).Value = Arr(UBound(Arr) - 1)
                    Case "6083"          'SN St Dev
                    Ws.Cells(Rw, 12).Value = Arr(UBound(Arr) - 1)
                    Case "6084"          'Speed Avg
                    Ws.Cells(Rw, 8).Value = Arr(UBound(Arr) - 1)
                    End Select
            End If
            End If
        Loop
Close #Fno

Next MyFile

End With

End Sub
子测试()
Dim Fno为整数,Ln为字符串,Arr为变量
Dim TestNo作为字符串,TestID作为字符串,轮子作为字符串
变暗Rw为长
将Ws设置为工作表
Dim MyFile作为变量
设置Ws=ThisWorkbook.ActiveSheet
使用Application.FileDialog(msoFileDialogFilePicker)
.AllowMultiSelect=True
.Filters.Add“Text”,“*.Txt”,1
如果.Show-1,则退出Sub
Rw=6
对于.SelectedItems中的每个MyFile
Fno=FreeFile
以#Fno的形式打开MyFile进行输入
TestID=“”
TestNo=“”
Wheel=“”
在文件结束前不要执行EOF(Fno)循环。
行输入#Fno,Ln'将行读入变量。
Ln=纵倾(Ln)
'将多个空间转换为单个空间以进行拆分
直到仪表(1,Ln,“”)=0为止
Ln=替换(Ln,“,”)
Arr=拆分(Ln,“”)
环
如果UBound(Arr)>2,则
选择案例Arr(1)
案例“5000”测试编号
如果TestID Arr(0)或TestNo Arr(UBound(Arr)),则
Rw=Rw+1
TestID=Arr(0)
TestNo=Arr(UBound(Arr))
Ws.Cells(Rw,1).Value=TestID
Ws.Cells(Rw,2).Value=TestNo
如果结束
案例“5005”距离
Ws.Cells(Rw,3).Value=Arr(UBound(Arr)-1)
案例“5006”启动Hr
Ws.Cells(Rw,4).Value=Arr(UBound(Arr)-1)
案例“5007”最小启动时间
Ws.Cells(Rw,5).Value=Arr(UBound(Arr)-1)
“5008”轮箱
车轮=Arr(UBound(Arr))
Ws.Cells(Rw,6.Value=Arr(UBound(Arr))
箱“5009”湿干
Ws.Cells(Rw,7).Value=Arr(UBound(Arr))
案件“5010”Lat
Ws.Cells(Rw,14.Value=Arr(UBound(Arr)-1)和“&Arr(UBound(Arr))
案件“5011”长
Ws.Cells(Rw,15.Value=Arr(UBound(Arr)-1)和“&Arr(UBound(Arr))
外壳“6001”表面温度
Ws.Cells(Rw,13).Value=Arr(UBound(Arr)-1)
结束选择
如果Wheel=“Right”,则
选择案例Arr(1)
案例“6060”序号平均值
Ws.Cells(Rw,9).Value=Arr(UBound(Arr)-1)
案例“6061”序号最小值
Ws.Cells(Rw,10).Value=Arr(UBound(Arr)-1)
案例“6062”序号最大值
Ws.Cells(Rw,11).Value=Arr(UBound(Arr)-1)
案例“6063”序号St Dev
Ws.Cells(Rw,12).Value=Arr(UBound(Arr)-1)
案例“6064”平均速度
Ws.Cells(Rw,8).Value=Arr(UBound(Arr)-1)
结束选择
其他的
选择案例Arr(1)
案例“6080”序号平均值
Ws.Cells(Rw,9).Value=Arr(UBound(Arr)-1)
案例“6081”序号最小值
Ws.Cells(Rw,10).Value=Arr(UBound(Arr)-1)
外壳“6082”序号最大值
Ws.Cells(Rw,11).Value=Arr(UBound(Arr)-1)
案件“6083”序号St Dev
Ws.Cells(Rw,12).Value=Arr(UBound(Arr)-1)
案例“6084”平均速度
Ws.Cells(Rw,8).Value=Arr(UBound(Arr)-1)
结束选择
如果结束
如果结束
环
关闭#Fno
下一个MyFile
以
端接头

这是一个框架代码,可以根据您的要求进行修改。将输入文本文件视为具有多个空格的固定宽度。使用提供的样本数据进行测试。为了简单起见,对输出表结构进行了如下修改

代码直接处理文本文件

Sub test()
Dim Fno As Integer, Ln As String, Arr As Variant
Dim TestNo As String, TestID As String, Wheel As String
Dim Rw As Long
Dim Ws As Worksheet
Dim MyFile As Variant

Set Ws = ThisWorkbook.ActiveSheet

With Application.FileDialog(msoFileDialogFilePicker)
   .AllowMultiSelect = True
   .Filters.Add "Text", "*.Txt", 1

    If .Show <> -1 Then Exit Sub
    Rw = 6

    For Each MyFile In .SelectedItems
    Fno = FreeFile

    Open MyFile For Input As #Fno

    TestID = ""
    TestNo = ""
    Wheel = ""

        Do While Not EOF(Fno)    ' Loop until end of file.
        Line Input #Fno, Ln    ' Read line into variable.
        Ln = Trim(Ln)

        ' multiple spaces are converted to single space for splitting
            Do Until InStr(1, Ln, "  ") = 0
            Ln = Replace(Ln, "  ", " ")
            Arr = Split(Ln, " ")
            Loop

            If UBound(Arr) > 2 Then
                Select Case Arr(1)
                Case "5000"        'TestNo
                    If TestID <> Arr(0) Or TestNo <> Arr(UBound(Arr)) Then
                    Rw = Rw + 1
                    TestID = Arr(0)
                    TestNo = Arr(UBound(Arr))
                    Ws.Cells(Rw, 1).Value = TestID
                    Ws.Cells(Rw, 2).Value = TestNo
                    End If
                Case "5005"   'Distance
                Ws.Cells(Rw, 3).Value = Arr(UBound(Arr) - 1)
                Case "5006"   'Start Hr
                Ws.Cells(Rw, 4).Value = Arr(UBound(Arr) - 1)
                Case "5007"    'Start min
                Ws.Cells(Rw, 5).Value = Arr(UBound(Arr) - 1)
                Case "5008"    'Wheel
                Wheel = Arr(UBound(Arr))
                Ws.Cells(Rw, 6).Value = Arr(UBound(Arr))
                Case "5009"    'WetDry
                Ws.Cells(Rw, 7).Value = Arr(UBound(Arr))
                Case "5010"    'Lat
                Ws.Cells(Rw, 14).Value = Arr(UBound(Arr) - 1) & " " & Arr(UBound(Arr))
                Case "5011"    'Long
                Ws.Cells(Rw, 15).Value = Arr(UBound(Arr) - 1) & " " & Arr(UBound(Arr))
                Case "6001"    'Surface Temp
                Ws.Cells(Rw, 13).Value = Arr(UBound(Arr) - 1)
                End Select

                If Wheel = "Right" Then
                    Select Case Arr(1)
                    Case "6060"          'SN Average
                    Ws.Cells(Rw, 9).Value = Arr(UBound(Arr) - 1)
                    Case "6061"          'SN Min
                    Ws.Cells(Rw, 10).Value = Arr(UBound(Arr) - 1)
                    Case "6062"          'SN MAx
                    Ws.Cells(Rw, 11).Value = Arr(UBound(Arr) - 1)
                    Case "6063"          'SN St Dev
                    Ws.Cells(Rw, 12).Value = Arr(UBound(Arr) - 1)
                    Case "6064"          'Speed Avg
                    Ws.Cells(Rw, 8).Value = Arr(UBound(Arr) - 1)
                    End Select
                Else
                    Select Case Arr(1)
                    Case "6080"          'SN Average
                    Ws.Cells(Rw, 9).Value = Arr(UBound(Arr) - 1)
                    Case "6081"          'SN Min
                    Ws.Cells(Rw, 10).Value = Arr(UBound(Arr) - 1)
                    Case "6082"          'SN MAx
                    Ws.Cells(Rw, 11).Value = Arr(UBound(Arr) - 1)
                    Case "6083"          'SN St Dev
                    Ws.Cells(Rw, 12).Value = Arr(UBound(Arr) - 1)
                    Case "6084"          'Speed Avg
                    Ws.Cells(Rw, 8).Value = Arr(UBound(Arr) - 1)
                    End Select
            End If
            End If
        Loop
Close #Fno

Next MyFile

End With

End Sub
子测试()
Dim Fno为整数,Ln为字符串,Arr为变量
Dim TestNo作为字符串,TestID作为字符串,轮子作为字符串
变暗Rw为长
将Ws设置为工作表
Dim MyFile作为变量
设置Ws=ThisWorkbook.ActiveSheet
使用Application.FileDialog(msoFileDialogFilePicker)
.AllowMultiSelect=True
.Filters.Add“Text”,“*.Txt”,1
如果.Show-1,则退出Sub
Rw=6
对于.SelectedItems中的每个MyFile
Fno=FreeFile
以#Fno的形式打开MyFile进行输入
TestID=“”
TestNo=“”
Wheel=“”
在文件结束前不要执行EOF(Fno)循环。
行输入#Fno,Ln'将行读入变量。
Ln=纵倾(Ln)
'将多个空间转换为单个空间以进行拆分
直到仪表(1,Ln,“”)=0为止
Ln=替换(Ln,“,”)
Arr=拆分(Ln,“”)
环