【问题标题】:Importing multiple text files into the same sheet将多个文本文件导入同一张工作表
【发布时间】:2014-01-20 03:42:22
【问题描述】:

我可以使用以下代码将一个文本文件导入 Excel。

Sub test()
    Sheet1.Cells(1, 1) = "Time"
    Sheet1.Cells(1, 2) = "QueueName"
    Sheet1.Cells(1, 3) = "Count"
    With ActiveSheet.QueryTables.Add(Connection:= _
        "TEXT;C:\temp\Sample.txt", Destination:=Range("$A$2") _
        )**strong text**
        .Name = "Sample"
        .FieldNames = False
        .RowNumbers = False
        .FillAdjacentFormulas = False
        .PreserveFormatting = True
        .RefreshOnFileOpen = True
        .RefreshStyle = xlInsertDeleteCells
        .SavePassword = False
        .SaveData = True
        .AdjustColumnWidth = True
        .RefreshPeriod = 0
        .TextFilePromptOnRefresh = False
        .TextFilePlatform = 437
        .TextFileStartRow = 1
        .TextFileParseType = xlDelimited
        .TextFileTextQualifier = xlTextQualifierDoubleQuote
        .TextFileConsecutiveDelimiter = True
        .TextFileTabDelimiter = False
        .TextFileSemicolonDelimiter = False
        .TextFileCommaDelimiter = False
        .TextFileSpaceDelimiter = True
        .TextFileColumnDataTypes = Array(1, 1, 1, 1, 1, 1)
        .TextFileTrailingMinusNumbers = True
        .Refresh BackgroundQuery:=False
    End With

如何更改它以导入 4 个不同的文本文件,以便将文件导入到单个工作表中,所有数据按以下顺序对齐

文件 1 数据 文件 2 数据 文件 3 数据 文件4数据

表示文件 2 数据应从文件 1 数据结束的位置开始。
文件 1 的数据可能会有所不同。所以不知道文件2数据的起始范围。
Destination:=Range("$A$2") 应该是什么?

【问题讨论】:

  • 更正文件2的数据应该从文件1的数据下面开始
  • 更正文件2的数据应该从文件1的数据下面开始
  • 我在下面添加了一个示例。将您的 filefilter 更改为您正在加载的 Textfile。此外,加载文件的顺序取决于您选择它们​​的顺序。如果您先按文件名排列文件,然后将它们全部选中,那就更好了。我还没有测试过很多样品,我把它留给你。

标签: excel vba


【解决方案1】:

您可以像这样使用GetOpenFilename excel 属性:

Sub Sample()

Dim myfiles
Dim i As Integer

myfiles = Application.GetOpenFilename(filefilter:="CSV Files (*.csv), *.csv", MultiSelect:=True)

If Not IsEmpty(myfiles) Then
    For i = LBound(myfiles) To UBound(myfiles)
         With ActiveSheet.QueryTables.Add(Connection:= _
            "TEXT;" & myfiles(i), Destination:=Range("A" & Rows.Count).End(xlUp).Offset(1, 0))
            .Name = "Sample"
            .FieldNames = False
            .RowNumbers = False
            .FillAdjacentFormulas = False
            .PreserveFormatting = True
            .RefreshOnFileOpen = True
            .RefreshStyle = xlInsertDeleteCells
            .SavePassword = False
            .SaveData = True
            .AdjustColumnWidth = True
            .RefreshPeriod = 0
            .TextFilePromptOnRefresh = False
            .TextFilePlatform = 437
            .TextFileStartRow = 1
            .TextFileParseType = xlDelimited
            .TextFileTextQualifier = xlTextQualifierDoubleQuote
            .TextFileConsecutiveDelimiter = True
            .TextFileTabDelimiter = False
            .TextFileSemicolonDelimiter = False
            .TextFileCommaDelimiter = False
            .TextFileSpaceDelimiter = True
            .TextFileColumnDataTypes = Array(1, 1, 1, 1, 1, 1)
            .TextFileTrailingMinusNumbers = True
            .Refresh BackgroundQuery:=False
        End With
    Next i
Else
    MsgBox "No File Selected"
End If

End Sub

希望这对你有用。

【讨论】:

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