【问题标题】:Import multiple CSV files into master excel spreadsheet and append the data to previous imports将多个 CSV 文件导入到主 excel 电子表格中,并将数据附加到以前的导入中
【发布时间】:2016-02-18 09:01:53
【问题描述】:

我有一个将 .csv 文件导入工作表“数据”的脚本

第一次导入后,所有后续的都出现在先前导入的右侧,而不是添加到底部(最后一行)。

我认为问题涉及脚本的这个区域: 目的地:=ThisWorkbook.Sheets("Data").Range("$A$1"))

Sub load_csv()
    Dim fStr As String

    With Application.FileDialog(msoFileDialogFilePicker)
        .Show
        If .SelectedItems.Count = 0 Then
            MsgBox "Cancel Selected"
            Exit Sub
        End If
        'fStr is the file path and name of the file you selected.
        fStr = .SelectedItems(1)
    End With

    With ThisWorkbook.Sheets("Data").QueryTables.Add(Connection:= _
    "TEXT;" & fStr, Destination:=ThisWorkbook.Sheets("Data").Range("$A$1"))
        .Name = "CAPTURE"
        .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 = True
        .TextFileSpaceDelimiter = False
        .TextFileColumnDataTypes = Array(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1)
        .TextFileTrailingMinusNumbers = True
        .Refresh BackgroundQuery:=False

    End With
End Sub

【问题讨论】:

  • 您是否查看过本网站上已经解决此问题的答案?

标签: vba excel csv


【解决方案1】:

您需要检查目的地以查看下一个未使用的单元格是什么。

With ThisWorkbook.Sheets("Data")
    .QueryTables.Add(Connection:= _
  ..., Destination:=.Cells(Rows.Count, "A").End(xlUp).Offset(1, 0)
'lots of other stuff
End With

您可以重复引用 ThisWorkbook.Sheets("Data") 但使用 With ... End With statement 更容易。

【讨论】:

    猜你喜欢
    • 2011-12-23
    • 1970-01-01
    • 2020-08-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多