2017-06-06 82 views
1

下面的代码无法正常工作和投掷错误复制CSV文件到Excel中使用录制的宏

无效的过程调用或参数。

这是将CSV文件数据导入Excel文件的记录宏代码。

Sub CopyCSVtoExcel() 
' 
' CopyCSVtoExcel Macro 
' 
' 
    With ActiveSheet.QueryTables.Add(Connection:= _ 
     "TEXT;C:\Users\hkum29\Desktop\New folder\data.csv", Destination:=Range("$A$1" _ 
     )) 
     .CommandType = 0 
     .Name = "data" 
     .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 = xlTextQualifierSingleQuote 
     .TextFileConsecutiveDelimiter = False 
     .TextFileTabDelimiter = False 
     .TextFileSemicolonDelimiter = False 
     .TextFileCommaDelimiter = True 
     .TextFileSpaceDelimiter = False 
     .TextFileColumnDataTypes = Array(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, _ 
     1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 _ 
     , 1, 1, 1, 1, 1, 1, 1) 
     .TextFileTrailingMinusNumbers = True 
     .Refresh BackgroundQuery:=True 
    End With 
End Sub 
+0

我猜想,该文件不存在或已经有'A1'表。 – UGP

+0

它是否工作,如果你删除线'.Refresh BackgroundQuery:= True'? –

+0

即使在删除行后,也不行.Refresh BackgroundQuery:= True –

回答

0

按照MSDN的KB QueryTable.CommandType Property (Excel)

您可以设置CommandType属性只有在查询类型属性的查询表或数据透视表高速缓存的值是xlOLEDBQuery。

既然它既不是,只需评论行.CommandType = 0或删除该行。