VBA代码将多个文本文件行导入单个excel工作表的列

时间:2019-05-15 06:24:30

标签: excel vba

我一直在尝试找出如何使用数百个以逗号和制表符分隔的文本文件,以及如何将特定的行值导入单个excel工作表的列中。 文本文件的格式为标准格式,对于每组测试结果,我要导入的行均编号为(ID)。开始导入的数据在所有文本文件中都是相同的行号,并且应基于“测试号ID”(即测试1,2,3 ....)循环记录。 理想情况下,代码应读取并循环遍历,然后仅导入指定的行号。也就是说,如果“ A” ='5000'复制测试编号(“ B”),则导入后续行

RowIDs 
6080
6081
6082
6083
6084
6085

对于每个带有'Test_ID'的行,都是文本文件名,从而形成一个像bewlo这样的表

Test_ID Test_No DistanceTime    Wheel   WetDry  Speed   AvgFN   MinFN       
HWY_1   1   0.692   11:55   L   W   100 43  10  
HWY_1   2   1.692   11:56   L   W   98  44  14
HWY_1   3   2.692   11:57   L   W   98  45  40  

非常感谢您的帮助!

我从kutools尝试了以下代码,它仅导入所有文件内容,包括我要排除的行。我尝试下面的读取数据代码可以到达任何地方。下面的示例文件的一部分,行号5100在“ B”(即“蒙特利尔”)中包含TestID。不需要行5101到5222(行93)。行从第94行(5000)开始循环遍历

5100,FILE NAME & SYSTEM MODEL              : Montreal.sks  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
5169,SELECTED PUMP(S)                      : LEFT
5170,AD SAMPLING RATE                 (hz) : 100
5200,Route                                 : Some_Route
5201,Road Bed                              : Some_Road_Bed
5202,Lane                                  : Some_Lane
5203,Starting RM                           : Some_Starting_RM
5204,Offset                                : Some_Offset
5205,District                              : Some_District
5206,County                                : Some_County
5207,Testing Purpose                       : Some_Testing_Purpose
5208,Operator                              : Some_Operator
5209,Driver                                : Some_Driver
5210,Responsible                           : Some_Responsible
5211,Vehicle Type                          : Some_Vehicle_Type
5212,Network                               : Some_Network
5213,Vehicle ID                            : Some_Vehicle_ID
5214,Vehicle VIN                           : Some_Vehicle_VIN
5215,Certification Date (YYYYMMDD)         : Some_Certification_Date
5216,Certification Code                    : Some_Certification_ Code
5217,Test Tire                             : Some_Test_Tire
5218,General Comment 1                     : Some_General_Comment_1
5219,General Comment 2                     : Some_General_Comment_2
5220,General Comment 3                     : Some_General_Comment_3
5221,General Comment 4                     : Some_General_Comment_4
5222,General Comment 5                     : Some_General_Comment_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
6006, Tank 1 Water Temperature         , 0.0           , f
6007, Tank 2 Water Temperature         , not installed , f
6008, Tank 1 Water Level               , 325.9         , 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             , 1029.24       , lb
6041, Left  Load Force                 , 949.52        , lb
6042, Left  Vehicle Speed              , 37.5          , mph
6043, Left  Test Wheel Speed           , 33.1          , mph
6044, Left  SN                         , 108.40        , friction
6045, Left  Percent Slip               , 11.6          , percent
6046, Left  Time to Peak Brake Coef    , 1.13          , seconds
6047, Left  Time to Wheel Lockup       , 1.37          , seconds
6048, Left  Flow Rate                  , 0.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                 , 84.2          , friction
6081, Left  SN Minimum                 , 78.3          , friction
6082, Left  SN Maximum                 , 89.4          , friction
6083, Left  SN Standard Deviation      , 2.7           , friction
6084, Left  SN Speed Average           , 36.6          , mph
6085, Left  SN Flow Average            , 0.0           , gpm
0000,=================================================================
XXXX,^^^^ Speed ^^^^
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,=================================================================

代码

Dim fso As New FileSystemObject
Dim fl As file
Dim fd As folder

Dim appFileName As String

Dim flFullName As String

Dim projectID As String
Dim hwy As String
Dim Dirn As String
Dim lane As String
Dim testDate As String
Dim From_desc As String
Dim To_desc As String
Dim LHRS As String
Dim offset As Single
Dim temp As String
Dim contractDetail As String
Dim testTime(1 To 1000) As String

Dim test_No(1 To 1000) As Integer
Dim distance(1 To 1000) As Double

Dim wheel(1 To 1000) As String
Dim wetDry(1 To 1000) As String
Dim avgSN(1 To 1000) As Single
Dim minSN(1 To 1000) As Single
Dim maxSN(1 To 1000) As Single
Dim speed(1 To 1000) As Single
Dim avgTemp(1 To 1000) As Single
Dim events(1 To 1000) As String
Dim flow(1 To 1000) As Double
Dim gps_lat(1 To 1000) As String
Dim gps_long(1 To 1000) As String
Sub test()
'UpdatebyKutoolsforExcel20151214
    Dim xSht  As Worksheet
    Dim xWb As Workbook
    Dim xStrPath As String
    Dim xFileDialog As FileDialog
    Dim xFile As String
    On Error GoTo ErrHandler
    Set xFileDialog = Application.FileDialog(msoFileDialogFolderPicker)
    xFileDialog.AllowMultiSelect = False
    xFileDialog.Title = "Select a folder [Kutools for Excel]"
    If xFileDialog.Show = -1 Then
        xStrPath = xFileDialog.SelectedItems(1)
    End If
    If xStrPath = "" Then Exit Sub
    Set xSht = ThisWorkbook.ActiveSheet
    If MsgBox("Clear the existing sheet before importing?", vbYesNo, "Kutools for Excel") = vbYes Then xSht.UsedRange.Clear
    Application.ScreenUpdating = False
    xFile = Dir(xStrPath & "\" & "*.sks")
    Do While xFile <> ""
        Set xWb = Workbooks.Open(xStrPath & "\" & xFile)
        Columns(1).Insert xlShiftToRight
        Columns(1).SpecialCells(xlBlanks).value = ActiveSheet.Name
        ActiveSheet.UsedRange.Copy xSht.Range("A" & Rows.Count).End(xlUp).offset(1)
        xWb.Close False
        xFile = Dir
    Loop
    Application.ScreenUpdating = True
    Exit Sub
ErrHandler:
    MsgBox "no files csv", , "Kutools for Excel"
End Sub

期望在我追加新文本文件时输出表,每个带有'Test_ID'的一行是文本文件名,结果是一个类似下面的表;

Test_ID     Test_No DistanceTime    Wheel   WetDry  Speed   AvgFN   MinFN       
Montreal    1   0.692   11:55   L   W   100 43  10  
Montreal    2   1.692   11:56   L   W   98  44  14
Montreal    3   2.692   11:57   L   W   98  45  40

0 个答案:

没有答案