使用宏将定界文件导入Excel;运行时错误'1004'

时间:2019-11-01 02:06:23

标签: excel vba excel-2010

我有一个要导入空白Excel电子表格的CSV文件列表。 我尝试使用下面显示的代码导入一个文件,但出现运行时错误:

Run-time error '1004': Application-defined or object-defined error

该错误是由以下行触发的:

With ActiveSheet.QueryTables.Add(Connection:=strConnexn, Destination:=Range("$A$1"))

我在“ Importing Delimited File to Excel with Macro (.CommandType = 0)”处看到了StackOverflow示例

在我无知的脑海中,宏“ subImportIntermediaCSVCallLogs_aaa”所阻塞的代码行与StackOverflow示例相同;但显然我弄错了。

尽管我通常可以弄清楚Word的vba,但是我没有使用Excel vba进行编码的经验,而且我也不知道在哪里寻找解释。

一旦运行宏“ subImportIntermediaCSVCallLogs_aaa(strCSV_Fullname)”,我的计划是使用宏“ subRunMacroOnSeveralCSV_files()”将一堆CSV文件导入同一电子表格,每次导入均在上一次导入下方开始4行。 如果您认为这样做不明智,请告诉我。

任何帮助将不胜感激。

这是我的代码:

Sub subImportIntermediaCSVCallLogs_aaa(strCSV_Fullname)
'
    Dim strConnexn As String

    strConnexn = Chr(34) & "TEXT;" & strCSV_Fullname & Chr(34)
    MsgBox strConnexn

    Application.CutCopyMode = False
    With ActiveSheet.QueryTables.Add(Connection:=strConnexn, Destination:=Range("$A$1"))
        .RowNumbers = False
        .FillAdjacentFormulas = False
        .RefreshOnFileOpen = False
        .RefreshStyle = xlInsertDeleteCells
        .SavePassword = False
        .SaveData = True
        .AdjustColumnWidth = True
        .RefreshPeriod = 0
        .TextFilePromptOnRefresh = False
        .TextFilePlatform = 437
        .TextFileStartRow = 1
        .TextFileTextQualifier = xlTextQualifierDoubleQuote
        .TextFileConsecutiveDelimiter = False
        .TextFileTabDelimiter = False
        .TextFileSemicolonDelimiter = False
        .TextFileCommaDelimiter = True
        .TextFileSpaceDelimiter = False
        .TextFileOtherDelimiter = False
        .PreserveFormatting = True       
        .FieldNames = True
        .TextFileColumnDataTypes = Array(1, 1, 1, 1, 3, 3, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1)
        .TextFileParseType = xlDelimited
        .TextFileTrailingMinusNumbers = True
        .Refresh BackgroundQuery:=False
    End With
    Range("A1,1:1048576").Select
    Columns("A:A").EntireColumn.AutoFit
    Columns("B:B").EntireColumn.AutoFit
    ActiveCell.SpecialCells(xlLastCell).Select

    ActiveSheet.Cells(ActiveSheet.Rows.Count, Selection.Column).End(xlUp).Select
    ActiveCell.Offset(4, 0).Select

End Sub ' End of subImportIntermediaCSVCallLogs_aaa(strCSV_Fullname)


Sub subRunMacroOnSeveralCSV_files()
    Dim strCSV_Fullname As String
    strCSV_Fullname = "E:\Intermedia\CallHistory\callhistory01.csv"
    strCSV_Fullname = "E:\Intermedia\CallHistory\callhistory02.csv"
    strCSV_Fullname = "E:\Intermedia\CallHistory\callhistory03.csv"
    subImportIntermediaCSVCallLogs_aaa strCSV_Fullname

End Sub ' End of subRunMacroOnSeveralCSV_files()

0 个答案:

没有答案