我是VBA编码的新手,我希望从测量设备中接收原始现场数据文本文件,我希望将其布置在新工作表的摘要表中;我有一个代码,可以通过将每个文件附加到单个表中来导入多个文本文件,但是原始数据文件包含太多摘要中不需要的行。在每个文本文件中输出的大约1000行中,我的摘要表仅需要16行。数据是根据每个测试编号排列的,因此“第2列”中的“ ROWID”与报告的指标相同。
我想帮助创建代码以在导入期间添加不带.txt的txt文件名,即从特定txt文件导入的记录的第1列每一行上的“ Filename1”,并基于“ Column 1”遍历原始数据表'作为'TestID','第2列'作为'ROWID',然后将'第4列'中的值复制到摘要表中,下面是示例输出, -还希望代码仅将新导入的txt文件附加在同一工作表的最后一行。 @Ahamed AU非常感谢您的帮助!
5100,FILE NAME & SYSTEM MODEL : Z:\FRICTION\FRICTION_2019\TESTING\test_file\filename1.txt 1295 PAVEMENT FRICTION TESTER (UNIVERSAL)
5101,SOFTWARE VERSION : 1.0.6506.22298
5101A,VCU SOFTWARE VERSION : 8000908.601.007a
5102,TIME (hh:mm:ss) : 11:44:13
5103,DATE (mm/dd/yyyy) : 01/22/2019
5104,SYSTEM UNITS : US (ENGLISH)
5105,TEST TYPE : STANDARD
5106,TARGET SPEED (mph) : 40.0
5107,TEST WHEEL : LEFT
5108,TEST LUBRICANT : WET
5109,DATA FILTER : 5 POINTS
5110,CYCLE TYPE : MANUAL
5111,CYCLE DWELL DISTANCE (ft) : 528.0
5112,WATER CONTROL : WATER OFF AFTER TEST
5113,NOZZLE CONTROL : RAISE NOZZLE ON BACKUP
5114,TIME PARAMETER - T1 (seconds) : 0.500
5115,TIME PARAMETER - T2 (seconds) : 2.000
5116,TIME PARAMETER - T3 (seconds) : 0.500
5117,TIME PARAMETER - T4 (seconds) : 1.000
5118,TIME PARAMETER - T5 (seconds) : 0.100
5145,VEHICLE ENCODER SF (in/pulse) : 0.47803
5146,LEFT ENCODER SF (in/pulse) : 0.50493
5147,RIGHT ENCODER SF (in/pulse) : 0.49165
5148,ENCODER CAL LENGTH (ft) : 1056.0
5149,ENCODER CALIBRATION TIME (hh:mm:ss) : 13:39:28
5150,ENCODER CALIBRATION DATE (mm/dd/yyyy) : 01/09/2019
5151,TRAILER WHEEL WEIGHT (lb) : 1085.00
5152,FORCE TRANSDUCERS : LEFT
5153,LEFT TRACTION CAL VALUE (lb) : 455.00
5154,LEFT LOAD CAL VALUE (lb) : 466.00
5155,LEFT TRACTION GAIN (lb/volt) : 500.607
5156,LEFT TRACTION OFFSET (lb) : -11.542
5157,LEFT LOAD GAIN (lb/volt) : 499.589
5158,LEFT LOAD OFFSET (lb) : -23.584
5159,RIGHT TRACTION CAL VALUE (lb) : 450.00
5160,RIGHT LOAD CAL VALUE (lb) : 450.00
5161,RIGHT TRACTION GAIN (lb/volt) : 495.000
5162,RIGHT TRACTION OFFSET (lb) : 6.199
5163,RIGHT LOAD GAIN (lb/volt) : 495.000
5164,RIGHT LOAD OFFSET (lb) : 18.799
5165,SYSTEM CALIBRATION TIME (hh:mm:ss) : 11:38:03
5166,SYSTEM CALIBRATION DATE (mm/dd/yyyy) : 01/22/2019
5167,SPEED RANGE TOLERANCE (+/- mph) : 1.5
5168,WATER DEPTH SETTING (mm) : 0.5
0000,=================================================================
0000, Test Summary Values , Value , Units
0000,-----------------------------------------------------------------
5000, Test Number , 1 ,
5001, Route , ,
5002, Direction , ,
5003, Lane , ,
5004, Event , ,
5005, Test Start DMI , 0.00 , ft
5006, Test Start Time (Hr) , 11 , hr
5007, Test Start Time (Min) , 40 , minute
5008, Test Wheel , Left ,
5009, Test Wet/Dry , WET ,
5010, Latitude , 034.0564687° N,
5011, Longitude , 084.3002236° W,
5012, Bearing , 179.9 , degrees
6000, Air Temperature , -3.5 , f
6001, Surface Temperature , 55.6 , f
6002, Left Tire Temperature , not installed , f
6003, Left Nozzle Water Temperature , 0.0 , f
6004, Right Tire Temperature , not installed , f
6005, Right Nozzle Water Temperature , not installed , f
5100,FILE NAME & SYSTEM MODEL : Filename2.txt 1295 PAVEMENT FRICTION TESTER (UNIVERSAL)
5101,SOFTWARE VERSION : 1.0.6506.22298
5101A,VCU SOFTWARE VERSION : 8000908.601.007a
5102,TIME (hh:mm:ss) : 11:44:13
5103,DATE (mm/dd/yyyy) : 01/22/2019
5104,SYSTEM UNITS : US (ENGLISH)
5105,TEST TYPE : STANDARD
5106,TARGET SPEED (mph) : 40.0
5107,TEST WHEEL : LEFT
5108,TEST LUBRICANT : WET
5109,DATA FILTER : 5 POINTS
5110,CYCLE TYPE : MANUAL
5111,CYCLE DWELL DISTANCE (ft) : 528.0
5112,WATER CONTROL : WATER OFF AFTER TEST
5113,NOZZLE CONTROL : RAISE NOZZLE ON BACKUP
5114,TIME PARAMETER - T1 (seconds) : 0.500
5115,TIME PARAMETER - T2 (seconds) : 2.000
5116,TIME PARAMETER - T3 (seconds) : 0.500
5117,TIME PARAMETER - T4 (seconds) : 1.000
5118,TIME PARAMETER - T5 (seconds) : 0.100
5119,DMI UNITS : FEET
5120,DMI COUNT DIRECTION : UP
5121,DMI REFERENCE START (ft) : 0.000
5122,START MODE : START TEST BY PENDANT AT DMI START
5123,START DISTANCE (ft) : 100.000
5124,DISPLAY PARAMETER : SN AVERAGE
5125,DISPLAY SETTING : TEST SAMPLES GRAPH
5126,SUMMARY DATA STORAGE : ON
5127,RAW DATA STORAGE : ON
5128,PRINTING : OFF
5129,PUMP SOURCE : LEFT PUMP = TANK 1 RIGHT PUMP = TANK 1
5130,FLOW METER 1 GAIN (gpm/volt) : 9.500
5131,FLOW METER 1 OFFSET (gpm) : 0.000
5134,AIR TEMPERATURE GAIN (f/volt) : 13.999
5135,AIR TEMPERATURE OFFSET (f) : 0.001
5136,SURFACE TEMPERATURE GAIN (f/volt) : 22.500
5137,SURFACE TEMPERATURE OFFSET (f) : -85.000
5140,GPS INSTALLED : YES
5141A,TANK1 TEMPERATURE GAIN (f/volt) : 21.996
5141B,TANK1 TEMPERATURE OFFSET (f) : 0.001
5145,VEHICLE ENCODER SF (in/pulse) : 0.47803
5146,LEFT ENCODER SF (in/pulse) : 0.50493
5147,RIGHT ENCODER SF (in/pulse) : 0.49165
5148,ENCODER CAL LENGTH (ft) : 1056.0
5149,ENCODER CALIBRATION TIME (hh:mm:ss) : 13:39:28
5150,ENCODER CALIBRATION DATE (mm/dd/yyyy) : 01/09/2019
5151,TRAILER WHEEL WEIGHT (lb) : 1085.00
5152,FORCE TRANSDUCERS : LEFT
5153,LEFT TRACTION CAL VALUE (lb) : 455.00
5154,LEFT LOAD CAL VALUE (lb) : 466.00
5155,LEFT TRACTION GAIN (lb/volt) : 500.607
5156,LEFT TRACTION OFFSET (lb) : -11.542
5157,LEFT LOAD GAIN (lb/volt) : 499.589
5158,LEFT LOAD OFFSET (lb) : -23.584
5159,RIGHT TRACTION CAL VALUE (lb) : 450.00
5160,RIGHT LOAD CAL VALUE (lb) : 450.00
5161,RIGHT TRACTION GAIN (lb/volt) : 495.000
5162,RIGHT TRACTION OFFSET (lb) : 6.199
5163,RIGHT LOAD GAIN (lb/volt) : 495.000
5164,RIGHT LOAD OFFSET (lb) : 18.799
5165,SYSTEM CALIBRATION TIME (hh:mm:ss) : 11:38:03
5166,SYSTEM CALIBRATION DATE (mm/dd/yyyy) : 01/22/2019
5167,SPEED RANGE TOLERANCE (+/- mph) : 1.5
5168,WATER DEPTH SETTING (mm) : 0.5
0000,=================================================================
0000, Test Summary Values , Value , Units
0000,-----------------------------------------------------------------
5000, Test Number , 2 ,
5001, Route , ,
5002, Direction , ,
5003, Lane , ,
5004, Event , ,
5005, Test Start DMI , 29.36 , ft
5006, Test Start Time (Hr) , 11 , hr
5007, Test Start Time (Min) , 41 , minute
5008, Test Wheel , Left ,
5009, Test Wet/Dry , WET ,
5010, Latitude , 034.0511896° N,
5011, Longitude , 084.3033369° W,
5012, Bearing , 201.9 , degrees
6000, Air Temperature , -1.6 , f
6001, Surface Temperature , 49.9 , f
6002, Left Tire Temperature , not installed , f
6003, Left Nozzle Water Temperature , 0.0 , f
6004, Right Tire Temperature , not installed , f
6005, Right Nozzle Water Temperature , not installed , f
6006, Tank 1 Water Temperature , 0.0 , f
6007, Tank 2 Water Temperature , not installed , f
6008, Tank 1 Water Level , 353.3 , gal
6009, Tank 2 Water Level , not installed , gal
6010, Data Filter , 5 , points
6011, Water Depth , 0.5 , mm
6012, Sampling Rate , 100 , hz
0000,-----------------------------------------------------------------
0000, Right Wheel Peak Values , Value , Units
0000,-----------------------------------------------------------------
6020, Right Traction Force , 0.00 , lb
6021, Right Load Force , 0.00 , lb
6022, Right Vehicle Speed , 0.0 , mph
6023, Right Test Wheel Speed , 0.0 , mph
6024, Right SN , 0.00 , friction
6025, Right Percent Slip , 0.0 , percent
6026, Right Time to Peak Brake Coef , 0.00 , seconds
6027, Right Time to Wheel Lockup , 0.00 , seconds
6028, Right Flow Rate , 0.0 , gpm
0000,-----------------------------------------------------------------
0000, Left Wheel Peak Values , Value , Units
0000,-----------------------------------------------------------------
6040, Left Traction Force , 880.60 , lb
6041, Left Load Force , 959.17 , lb
6042, Left Vehicle Speed , 40.5 , mph
6043, Left Test Wheel Speed , 36.0 , mph
6044, Left SN , 91.81 , friction
6045, Left Percent Slip , 11.2 , percent
6046, Left Time to Peak Brake Coef , 0.62 , seconds
6047, Left Time to Wheel Lockup , 0.76 , seconds
6048, Left Flow Rate , 29.1 , gpm
0000,-----------------------------------------------------------------
0000, Right Wheel SN Values , Value , Units
0000,-----------------------------------------------------------------
6060, Right SN Average , 0.0 , friction
6061, Right SN Minimum , 0.0 , friction
6062, Right SN Maximum , 0.0 , friction
6063, Right SN Standard Deviation , 0.0 , friction
6064, Right SN Speed Average , 0.0 , mph
6065, Right SN Flow Average , 0.0 , gpm
0000,-----------------------------------------------------------------
0000, Left Wheel SN Values , Value , Units
0000,-----------------------------------------------------------------
6080, Left SN Average , 53.4 , friction
6081, Left SN Minimum , 51.5 , friction
6082, Left SN Maximum , 55.5 , friction
6083, Left SN Standard Deviation , 0.9 , friction
6084, Left SN Speed Average , 40.0 , mph
6085, Left SN Flow Average , 28.8 , gpm
0000,=================================================================
0000,=================================================================
0000, Test Summary Values , Value , Units
0000,-----------------------------------------------------------------
5000, Test Number , 3 ,
5001, Route , ,
5002, Direction , ,
5003, Lane , ,
5004, Event , ,
5005, Test Start DMI , 411.62 , ft
5006, Test Start Time (Hr) , 11 , hr
5007, Test Start Time (Min) , 41 , minute
5008, Test Wheel , Left ,
5009, Test Wet/Dry , WET ,
5010, Latitude , 034.0501615° N,
5011, Longitude , 084.3038254° W,
5012, Bearing , 201.1 , degrees
6000, Air Temperature , -1.5 , f
6001, Surface Temperature , 54.6 , f
6002, Left Tire Temperature , not installed , f
6003, Left Nozzle Water Temperature , 0.0 , f
6004, Right Tire Temperature , not installed , f
6005, Right Nozzle Water Temperature , not installed , f
6006, Tank 1 Water Temperature , 0.0 , f
6007, Tank 2 Water Temperature , not installed , f
6008, Tank 1 Water Level , 326.0 , gal
6009, Tank 2 Water Level , not installed , gal
6010, Data Filter , 5 , points
6011, Water Depth , 0.5 , mm
6012, Sampling Rate , 100 , hz
0000,-----------------------------------------------------------------
0000, Right Wheel Peak Values , Value , Units
0000,-----------------------------------------------------------------
6020, Right Traction Force , 0.00 , lb
6021, Right Load Force , 0.00 , lb
6022, Right Vehicle Speed , 0.0 , mph
6023, Right Test Wheel Speed , 0.0 , mph
6024, Right SN , 0.00 , friction
6025, Right Percent Slip , 0.0 , percent
6026, Right Time to Peak Brake Coef , 0.00 , seconds
6027, Right Time to Wheel Lockup , 0.00 , seconds
6028, Right Flow Rate , 0.0 , gpm
0000,-----------------------------------------------------------------
0000, Left Wheel Peak Values , Value , Units
0000,-----------------------------------------------------------------
6040, Left Traction Force , 814.08 , lb
6041, Left Load Force , 907.37 , lb
6042, Left Vehicle Speed , 41.8 , mph
6043, Left Test Wheel Speed , 37.7 , mph
6044, Left SN , 89.72 , friction
6045, Left Percent Slip , 9.9 , percent
6046, Left Time to Peak Brake Coef , 0.54 , seconds
6047, Left Time to Wheel Lockup , 0.71 , seconds
6048, Left Flow Rate , 30.0 , gpm
0000,-----------------------------------------------------------------
0000, Right Wheel SN Values , Value , Units
0000,-----------------------------------------------------------------
6060, Right SN Average , 0.0 , friction
6061, Right SN Minimum , 0.0 , friction
6062, Right SN Maximum , 0.0 , friction
6063, Right SN Standard Deviation , 0.0 , friction
6064, Right SN Speed Average , 0.0 , mph
6065, Right SN Flow Average , 0.0 , gpm
0000,-----------------------------------------------------------------
0000, Left Wheel SN Values , Value , Units
0000,-----------------------------------------------------------------
6080, Left SN Average , 49.9 , friction
6081, Left SN Minimum , 48.4 , friction
6082, Left SN Maximum , 51.5 , friction
6083, Left SN Standard Deviation , 0.7 , friction
6084, Left SN Speed Average , 41.4 , mph
6085, Left SN Flow Average , 29.5 , gpm
0000,=================================================================
答案 0 :(得分:0)
可以尝试下面的代码
Sub testNewFormat()
Dim Fno As Integer, Ln As String, Arr As Variant, RawArr As Variant
Dim TestNo As String, TestID As String, Wheel As String
Dim PrvTestNo As String, PrvTestID As String
Dim Rw As Long, FileNameLen As Integer
Dim Ws As Worksheet
Dim MyFile As Variant
Set Ws = ThisWorkbook.ActiveSheet
With Application.FileDialog(msoFileDialogFilePicker)
.AllowMultiSelect = True
'Delete next line if file extention are not .sks
.Filters.Add "SKS Files", "*.sks", 1
If .Show <> -1 Then Exit Sub
Rw = 6
'Next line would append data in excel File instead of Overwriting
'Delete the line if want to overwrite Excel data
Rw = Ws.Range("A" & Rows.Count).End(xlUp).Row
For Each MyFile In .SelectedItems
Fno = FreeFile
Open MyFile For Input As #Fno
PrvTestID = ""
PrvTestNo = ""
TestID = ""
TestNo = ""
Wheel = ""
Do While Not EOF(Fno) ' Loop until end of file.
Line Input #Fno, Ln ' Read line into variable.
Arr = Split(Ln, ",")
If UBound(Arr) = 1 And Trim(Arr(0)) = "5100" And InStr(1, UCase(Arr(1)), ".TXT") > 0 And InStr(1, Arr(1), ":") > 0 Then 'Test ID or FileNAme
'FileName with Full Path wiyhout extension (.Txt)
If InStr(1, UCase(Arr(1)), ".TXT") > InStr(1, Arr(1), ":") Then
TestID = Mid(Arr(1), InStr(1, Arr(1), ":") + 1, InStr(1, UCase(Arr(1)), ".TXT") - InStr(1, Arr(1), ":") - 1)
TestID = Trim(TestID)
End If
'FileName Stripped off Full Path And extension (.Txt)
If InStr(1, TestID, "\") > 0 Then
TestID = Right(TestID, Len(TestID) - InStrRev(TestID, "\"))
End If
If PrvTestID <> TestID Then
Rw = Rw + 1
Ws.Cells(Rw, 1).Value = TestID
Ws.Cells(Rw, 2).Value = TestNo
PrvTestID = TestID
End If
End If
If UBound(Arr) >= 2 Then
Select Case Trim(Arr(0))
Case "5000" 'TestNo
TestNo = Arr(2)
If PrvTestNo <> TestNo Then
If PrvTestNo <> "" Then Rw = Rw + 1
Ws.Cells(Rw, 1).Value = TestID
Ws.Cells(Rw, 2).Value = TestNo
PrvTestNo = TestNo
End If
Case "5005" 'Distance
Ws.Cells(Rw, 3).Value = Arr(2)
Case "5006" 'Start Hr
Ws.Cells(Rw, 4).Value = Arr(2)
Case "5007" 'Start min
Ws.Cells(Rw, 5).Value = Arr(2)
Case "5008" 'Wheel
Wheel = Arr(2)
Ws.Cells(Rw, 6).Value = Wheel
Case "5009" 'WetDry
Ws.Cells(Rw, 7).Value = Arr(2)
Case "5010" 'Lat
Ws.Cells(Rw, 14).Value = Arr(2)
Case "5011" 'Long
Ws.Cells(Rw, 15).Value = Arr(2)
Case "6001" 'Surface Temp
Ws.Cells(Rw, 13).Value = Arr(2)
End Select
If Wheel = "Right" Then
Select Case Arr(0)
Case "6060" 'SN Average
Ws.Cells(Rw, 9).Value = Arr(2)
Case "6061" 'SN Min
Ws.Cells(Rw, 10).Value = Arr(2)
Case "6062" 'SN MAx
Ws.Cells(Rw, 11).Value = Arr(2)
Case "6063" 'SN St Dev
Ws.Cells(Rw, 12).Value = Arr(2)
Case "6064" 'Speed Avg
Ws.Cells(Rw, 8).Value = Arr(2)
End Select
Else
Select Case Arr(0)
Case "6080" 'SN Average
Ws.Cells(Rw, 9).Value = Arr(2)
Case "6081" 'SN Min
Ws.Cells(Rw, 10).Value = Arr(2)
Case "6082" 'SN MAx
Ws.Cells(Rw, 11).Value = Arr(2)
Case "6083" 'SN St Dev
Ws.Cells(Rw, 12).Value = Arr(2)
Case "6084" 'Speed Avg
Ws.Cells(Rw, 8).Value = Arr(2)
End Select
End If
End If
Loop
Close #Fno
Next MyFile
End With
End Sub
为使用文件名(无论是否包含完整路径)而编辑的代码。
样本数据输出