1

我刚刚熟悉了 VBA 语言,如果有任何帮助,我将不胜感激。我正在尝试编写一个使用新文件更新现有选项卡的程序。这是我到目前为止所拥有的:

Sub ImportFile()
'
' ImportFile Macro
'
' Keyboard Shortcut: Ctrl+Shift+I

    ' Clears active sheet
    Cells.Select
    Selection.ClearContents
    Range("A1").Select

    ' "Open" prompt window
    Dim aFile As String
    aFile = Application.GetOpenFilename(FileFilter:="Comma separated values files, *.csv")
    If aFile = "False" Then Exit Sub



    ' Import .csv file to active sheet
     With ActiveSheet.QueryTables.Add(Connection:= _
       "TEXT;C:\Users\jiarui.hou.THRUBIT\Desktop\thrubit\data\PS25R_175c_20110711_proc1.csv" _
        , Destination:=Range("$A$1"))
        .Name = "PS25R_175c_20110711_proc1"
        .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 = 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)
        .TextFileTrailingMinusNumbers = True
        .Refresh BackgroundQuery:=False
    End With
End Sub

但问题出在第三部分的前两行,它指向一个特定的文件。但我希望程序导入我特别选择的任何文件。我想我要问的是如何将提示窗口打开文件的第二部分链接到链接该目录地址而不是当前特定目录地址的第三部分。

4

1 回答 1

0

使用连接运算符&并使传递给连接的字符串成为"TEXT;" & aFile

 With ActiveSheet.QueryTables.Add(Connection:= "TEXT;" & aFile, Destination:=Range("$A$1"))

另外,我认为不是"False"你想要False的第 7 行。

于 2011-07-19T14:37:27.190 回答