Importing multiple text files using VBA Macro

后端 未结 1 785
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-22 13:45

I have a daily dump of 2 different text files (in the same folder) that get overwritten daily. I would like to be able to import them into an active spreadsheet with tab del

相关标签:
1条回答
  • 2020-12-22 14:02

    do like this if your text files is with tab delimited.

    Sub ReadFilesIntoActiveSheet()
    Dim fso As FileSystemObject
    Dim folder As folder
    Dim file As file
    Dim FileText As TextStream
    Dim TextLine As String
    Dim Items() As String
    Dim i As Long
    Dim cl As Range
    Dim sFolder As String, vDB, Ws As Worksheet
    Dim rngT As Range
    ' Get a FileSystem object
    Set fso = New FileSystemObject
    
        ' get the directory you want
    
        sFolder = "C:\Users\Mr D\Music\"
        Set folder = fso.GetFolder(sFolder)
        ' set the starting point to write the data to
        Set Ws = ActiveSheet
        'Set cl = ActiveSheet.Cells(1, 1)
    
        ' Loop thru all files in the folder
        For Each file In folder.Files
            Workbooks.Open Filename:=sFolder & file.Name, Format:=1
            With ActiveWorkbook.ActiveSheet
                vDB = .UsedRange
            End With
            ActiveWorkbook.Close
            Set rngT = Ws.Range("a" & Rows.Count).End(xlUp)(2)
            rngT.Resize(UBound(vDB, 1), UBound(vDB, 2)) = vDB
        Next file
        Ws.Range("a1").EntireRow.Delete
        Set FileText = Nothing
        Set file = Nothing
        Set folder = Nothing
        Set fso = Nothing
    
    End Sub
    

    From the second text file, the header will be ignored.

    Sub ReadFilesIntoActiveSheet()
    Dim fso As FileSystemObject
    Dim folder As folder
    Dim file As file
    Dim FileText As TextStream
    Dim TextLine As String
    Dim Items() As String
    Dim i As Long
    Dim cl As Range
    Dim sFolder As String, vDB, Ws As Worksheet
    Dim rngT As Range
    ' Get a FileSystem object
    Set fso = New FileSystemObject
    
        ' get the directory you want
    
        sFolder = "C:\Users\Mr D\Music\"
        Set folder = fso.GetFolder(sFolder)
        ' set the starting point to write the data to
        Set Ws = ActiveSheet
        'Set cl = ActiveSheet.Cells(1, 1)
        Ws.Cells.Clear
        ' Loop thru all files in the folder
        For Each file In folder.Files
            i = i + 1
            Workbooks.Open Filename:=sFolder & file.Name, Format:=1
            With ActiveWorkbook.ActiveSheet
                If i = 1 Then
                    vDB = .UsedRange
                Else
                    vDB = .UsedRange.Offset(1)
                End If
            End With
            ActiveWorkbook.Close
            Set rngT = Ws.Range("a" & Rows.Count).End(xlUp)(2)
            rngT.Resize(UBound(vDB, 1), UBound(vDB, 2)) = vDB
        Next file
        Ws.Range("a1").EntireRow.Delete
        Set FileText = Nothing
        Set file = Nothing
        Set folder = Nothing
        Set fso = Nothing
    
    End Sub
    
    0 讨论(0)
提交回复
热议问题