从Userform中选择文件后如何将CSV数据导入Excel

从Userform中选择文件后如何将CSV数据导入Excel,excel,vba,csv,Excel,Vba,Csv,我需要能够使用get external data函数将以逗号分隔的Excel工作表中的列数据导入到新工作表中。我最初希望选择userform中的文件,但无法确定如何将userform中选择的文件与Import命令匹配。导入模块的代码已附加 With ActiveSheet.QueryTables.Add(Connection:= _ "TEXT;C:\Users\---\Rainflow Data\J-Rain Outputs\Moment(N-mm).csv" _ , De

我需要能够使用get external data函数将以逗号分隔的Excel工作表中的列数据导入到新工作表中。我最初希望选择userform中的文件,但无法确定如何将userform中选择的文件与Import命令匹配。导入模块的代码已附加

  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
我需要它来打开我在userform中选择的文件,而不是引用录制此宏时选择的文件路径。任何帮助都将不胜感激


谢谢

假设变量
yourFilePath
具有所选文件的路径,则可以执行以下操作:

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

由于行“.CommandType=0”,宏将在excel 2013中失败,错误为“帮助:运行时错误5无效的过程调用或参数”。应删除该行。
yourFilePath=“C:\Users\--\Rainflow Data\J-Rain Outputs\Moment(N-mm).csv”
是否正确?