如何在从Userform选择文件后将CSV数据导入Excel [英] How to Import CSV data into Excel after selecting file from Userform

查看:237
本文介绍了如何在从Userform选择文件后将CSV数据导入Excel的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I need to be able to import column data from a Comma Delimited Excel Sheet into a new sheet using the get external data function. I wish to originally select the file in a userform but cannot figure out how to mate the file selected in the userform with the Import Command. Code for the import module is attached.

  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中选择的文件。

Instead of referencing the file path that I selected when recording this macro I need it to open the file I select in the userform. Any help would be greatly appreciated.

感谢

推荐答案

变量您的FilePath 有所选文件的路径您可以这样做:

Assuming the variable yourFilePath has the path to the selected file you can do this:

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

这篇关于如何在从Userform选择文件后将CSV数据导入Excel的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆