来自activecell VBA的开源路径文件

时间:2014-08-15 08:47:01

标签: excel vba excel-vba

"这段代码可以用任何路径打开源文件..但是现在我在sheet1上有源路径文件列表,我想在活动单元格上打开文件..我该如何修复这段代码? "

Dim Ret

Ret = Application.GetOpenFilename("All Files (*), *")

Sheet2.Activate

If Ret <> False Then
    With ActiveSheet.QueryTables.Add(Connection:= _
    "TEXT;" & Ret, Destination:=Range("$A$1"))

    .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 = False
    .TextFileSpaceDelimiter = False
    .TextFileOtherDelimiter = "|"
    .TextFileColumnDataTypes = Array(1, 1, 1, 1, 1)
    .TextFileTrailingMinusNumbers = True
    .Refresh BackgroundQuery:=False
End With
End If

1 个答案:

答案 0 :(得分:0)

ActiveCell 的值分配给Ret

Ret = ActiveCell.Value 

然后检查返回的路径是否有效。类似的东西:

If Dir(Ret) <> "" Then
    '~~> rest of your code here
Else
    MsgBox "Invalid Path"
    '~~> or do something else
End If

HTH。