宏粘贴文本打开文件

时间:2015-02-18 11:58:21

标签: excel excel-vba vba

我有以下宏从文本导入外部数据,将文本设置为逗号列,并将所有列设置为文本。我想修改下面的内容,以便打开一个消息框,提示打开文件,然后按照步骤操作。现在它正在使用我记录的文件路径和名称。

Cells.Select
    Selection.NumberFormat = "@"
    Range("A1").Select
    With ActiveSheet.QueryTables.Add(Connection:= _
        "TEXT;C:\------\FAC\FAC010515.txt", Destination:=Range( _
        "$A$1"))
        .Name = "FAC010515"
        .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(2, 2, 2, 2, 2, 2, 2, 2, 2)
        .TextFileTrailingMinusNumbers = True
        .Refresh BackgroundQuery:=False
    End With

1 个答案:

答案 0 :(得分:0)

此解决方案如何:Link

Dim intChoice As Integer 
Dim strPath As String 

'only allow the user to select one file 
Application.FileDialog(msoFileDialogOpen).AllowMultiSelect = False
'make the file dialog visible to the user 
intChoice = Application.FileDialog(msoFileDialogOpen).Show
'determine what choice the user made 
If intChoice <> 0 Then 
'get the file path selected by the user 
strPath = Application.FileDialog(msoFileDialogOpen).SelectedItems(1)