【问题标题】:Import multiple text files into single worksheet将多个文本文件导入单个工作表
【发布时间】:2018-07-09 15:13:04
【问题描述】:

我发现以下code 将每个文本文件导入到单独的工作表中,并且运行良好。有没有办法修改代码,以便将所有文本文件导入单个工作表?

如果有区别的话,我在 Windows7 64 位上使用 Excel 2013。

Sub ImportTXTFiles()
    Dim fso As Object
    Dim xlsheet As Worksheet
    Dim qt As QueryTable
    Dim txtfilesToOpen As Variant, txtfile As Variant

    Application.ScreenUpdating = False
    Set fso = CreateObject("Scripting.FileSystemObject")

    txtfilesToOpen = Application.GetOpenFilename _
                 (FileFilter:="Text Files (*.txt), *.txt", _
                  MultiSelect:=True, Title:="Text Files to Open")    

    For Each txtfile In txtfilesToOpen
        ' FINDS EXISTING WORKSHEET
        For Each xlsheet In ThisWorkbook.Worksheets
            If xlsheet.Name = Replace(fso.GetFileName(txtfile), ".txt", "") Then
                xlsheet.Activate
                GoTo ImportData
            End If
        Next xlsheet

        ' CREATES NEW WORKSHEET IF NOT FOUND
        Set xlsheet = ThisWorkbook.Worksheets.Add( _
                             After:=ThisWorkbook.Sheets(ThisWorkbook.Sheets.Count))
        xlsheet.Name = Replace(fso.GetFileName(txtfile), ".txt", "")
        xlsheet.Activate
        GoTo ImportData

ImportData:
        ' DELETE EXISTING DATA
        ActiveSheet.Range("A:Z").EntireColumn.Delete xlShiftToLeft

        ' IMPORT DATA FROM TEXT FILE
        With ActiveSheet.QueryTables.Add(Connection:="TEXT;" & txtfile, _
          Destination:=ActiveSheet.Cells(1, 1))
            .TextFileParseType = xlDelimited
            .TextFileConsecutiveDelimiter = False
            .TextFileTabDelimiter = False
            .TextFileSemicolonDelimiter = False
            .TextFileCommaDelimiter = False
            .TextFileSpaceDelimiter = False
            .TextFileOtherDelimiter = "|"

            .Refresh BackgroundQuery:=False
        End With

        For Each qt In ActiveSheet.QueryTables
            qt.Delete
        Next qt
    Next txtfile

    Application.ScreenUpdating = True
    MsgBox "Successfully imported text files!", vbInformation, "SUCCESSFUL IMPORT"

    Set fso = Nothing
End Sub

【问题讨论】:

    标签: vba excel


    【解决方案1】:

    这里的很多代码都在处理创建新标签等问题。

    剩下的是将每个文本文件加载到 Cells(1,1) 中的循环 - 因此,如果我们调整它以指向检查 A 列中最后使用的单元格的值,那么这应该做什么你需要:

    Sub ImportTXTFiles()
        Dim fso As Object
        Dim xlsheet As Worksheet
        Dim qt As QueryTable
        Dim txtfilesToOpen As Variant, txtfile As Variant
    
        Application.ScreenUpdating = False
        Set fso = CreateObject("Scripting.FileSystemObject")
    
        txtfilesToOpen = Application.GetOpenFilename _
                     (FileFilter:="Text Files (*.txt), *.txt", _
                      MultiSelect:=True, Title:="Text Files to Open")
    
        With ActiveSheet
    
            For Each txtfile In txtfilesToOpen
    
                importrow = 1 + .Cells(.Rows.Count, 1).End(xlUp).Row
    
                ' IMPORT DATA FROM TEXT FILE
                With .QueryTables.Add(Connection:="TEXT;" & txtfile, _
                  Destination:=.Cells(importrow, 1))
                    .TextFileParseType = xlDelimited
                    .TextFileConsecutiveDelimiter = False
                    .TextFileTabDelimiter = False
                    .TextFileSemicolonDelimiter = False
                    .TextFileCommaDelimiter = False
                    .TextFileSpaceDelimiter = False
                    .TextFileOtherDelimiter = "|"
                    .Refresh BackgroundQuery:=False
                End With
    
    
            Next txtfile
    
            For Each qt In .QueryTables
                qt.Delete
            Next qt
    
        End With
    
        Application.ScreenUpdating = True
        MsgBox "Successfully imported text files!", vbInformation, "SUCCESSFUL IMPORT"
    
        Set fso = Nothing
    End Sub
    

    另外,我注意到您删除了循环中的“所有”查询表。这是没有必要的。全部加载后将其全部删除。

    【讨论】:

    • 完美运行,谢谢!我对编码一无所知,所以这对我很有帮助。
    【解决方案2】:

    我相信以下内容会达到您的预期,这会将您的所有文本数据放入一个工作表中,它将检查 A 列中数据的最后一行,并偏移一行以从下一个文本导入数据文件:

    Sub ImportTXTFiles()
        Dim fso As Object
        Dim xlsheet As Worksheet
        Dim qt As QueryTable
        Dim LastRow As Long
        Dim txtfilesToOpen As Variant, txtfile As Variant
    
        Application.ScreenUpdating = False
        Set fso = CreateObject("Scripting.FileSystemObject")
    
        txtfilesToOpen = Application.GetOpenFilename _
                     (FileFilter:="Text Files (*.txt), *.txt", _
                      MultiSelect:=True, Title:="Text Files to Open")
    
        For Each txtfile In txtfilesToOpen
            LastRow = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row + 1
            ' IMPORT DATA FROM TEXT FILE
            With ActiveSheet.QueryTables.Add(Connection:="TEXT;" & txtfile, _
              Destination:=ActiveSheet.Cells(LastRow, 1))
                .TextFileParseType = xlDelimited
                .TextFileConsecutiveDelimiter = False
                .TextFileTabDelimiter = False
                .TextFileSemicolonDelimiter = False
                .TextFileCommaDelimiter = False
                .TextFileSpaceDelimiter = False
                .TextFileOtherDelimiter = "|"
    
                .Refresh BackgroundQuery:=False
            End With
    
            For Each qt In ActiveSheet.QueryTables
                qt.Delete
            Next qt
        Next txtfile
    
        Application.ScreenUpdating = True
        MsgBox "Successfully imported text files!", vbInformation, "SUCCESSFUL IMPORT"
    
        Set fso = Nothing
    End Sub
    

    【讨论】:

      猜你喜欢
      • 2016-04-17
      • 2015-06-13
      • 1970-01-01
      • 1970-01-01
      • 2023-03-21
      • 2021-01-27
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多