2014-02-12 36 views
0

我需要能够使用获取外部数据功能将逗号分隔Excel工作表中的列数据导入到新工作表中。我希望最初选择用户窗体中的文件,但无法弄清楚如何将用户窗体中选择的文件与导入命令相匹配。导入模块的代码已附加。如何从用户表单中选择文件后将CSV数据导入Excel

With ActiveSheet.QueryTables.Add(Connection:= _ 
    "TEXT;C:\Users\---\Rainflow Data\J-Rain Outputs\Moment(N-mm).csv" _ 
    , Destination:=Range("$A$1")) 
    .CommandType = 0 
    .Name = "Moment(N-mm)" 
    .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) 
    .TextFileTrailingMinusNumbers = True 
    .Refresh BackgroundQuery:=False 
End With 

而不是引用我记录这个宏,我需要它来打开我的窗体中选择文件时选择的文件路径。任何帮助将不胜感激。

感谢

+1

由于线” .CommandType = 0' 宏将在Excel 2013失败,错误“帮助:运行时错误5无效的过程调用或参数“。该行应该被删除。 – arberg

回答

1

假设变量yourFilePath有路径选择的文件,你可以这样做:

With ActiveSheet.QueryTables.Add(Connection:= "TEXT;" & yourFilePath, _ 
           Destination:=Range("$A$1")) 
    .CommandType = 0 
    .Name = "Moment(N-mm)" 
    .FieldNames = True 
    .RowNumbers = False 
    'etc 
+0

'yourFilePath =“C:\ Users \ --- \ Rainflow Data \ J-Rain Outputs \ Moment(N-mm).csv”'是否正确? –

相关问题