Importing 100 text files into Excel at once

前端 未结 2 499
一生所求
一生所求 2021-01-14 19:53

I have this macro to bulk import in a excel spreadsheet 100+ .txt files contained in the same folder :

Sub QueryImportText()
    Dim sPath As String, sName A         


        
2条回答
  •  暖寄归人
    2021-01-14 20:28

    Thanks a lot for this information. I wanted to import only 4th column of my data file for that I had to put bit modification as follows

     Sub QueryImportText()
        Dim sPath As String, sName As String
        Dim i As Long, qt As QueryTable
        With ThisWorkbook
            .Worksheets.Add After:= _
                .Worksheets(.Worksheets.Count)
        End With
        ActiveSheet.Name = Format(Now, "yyyymmdd_hhmmss")
        sPath = "C:\Users\TxtFiles\"
        sName = Dir(sPath & "*.txt")
        i = 0
        Do While sName <> ""
            i = i + 1
            Cells(1, i).Value = sName
            With ActiveSheet.QueryTables.Add(Connection:= _
                "TEXT;" & sPath & sName, Destination:=Cells(2, i))
                .Name = Left(sName, Len(sName) - 4)
                .FieldNames = True
                .RowNumbers = False
                .FillAdjacentFormulas = False
                .PreserveFormatting = True
                .RefreshOnFileOpen = False
                .RefreshStyle = xlInsertDeleteCells
                .SavePassword = False
                .SaveData = True
                .AdjustColumnWidth = True
                .RefreshPeriod = 0
                .TextFilePromptOnRefresh = False,
                .TextFilePlatform = 437
                .TextFileStartRow = 1
                .TextFileParseType = xlDelimited
                .TextFileTextQualifier = xlTextQualifierDoubleQuote
                .TextFileConsecutiveDelimiter = False
                .TextFileTabDelimiter = True
                .TextFileSemicolonDelimiter = False
                .TextFileCommaDelimiter = False
                .TextFileSpaceDelimiter = False
                .TextFileColumnDataTypes = Array(9,9,9,1) <---------(here)
                .TextFileTrailingMinusNumbers = True
                .Refresh BackgroundQuery:=False
            End With
            sName = Dir()
            For Each qt In ActiveSheet.QueryTables
                qt.Delete
            Next
        Loop
    End Sub
    

提交回复
热议问题