【问题标题】:VBA Excel prompt user to select a file in a default folderVBA Excel 提示用户选择默认文件夹中的文件
【发布时间】:2015-08-11 23:03:57
【问题描述】:

我想提示用户在默认文件夹中打开 Excel 文件。我不知道如何打开默认文件夹。

Sub Program1()
    DefaultFolder = "C:\user\dump"
    FName = Application.GetOpenFilename
    If FName <> False Then
        Set WorkB2 = Workbooks.Open(FName)
        ' some codes here
    End If
End Sub                           

【问题讨论】:

    标签: vba excel directory default


    【解决方案1】:

    使用 chdir:

    Sub Program1()
      Chdir "C:\user\dump"
      FName = Application.GetOpenFilename
      If FName <> False Then
        Set WorkB2 = Workbooks.Open(FName)
        ' some codes here
      End If
    End Sub
    

    【讨论】:

      【解决方案2】:

      这里是打开excel文件的例子

      Option Explicit
      Sub OpenFile()
          Dim xlFile      As Variant
      
          ChDir "C:\temp"
      
          '// Showing Excel Dialog
          xlFile = Application.GetOpenFilename("All Excel Files (*.xls*)," & _
          "*.xls*", 1, "Select Excel File", "Open", False)
      
          '// If Cancel then exit
          If TypeName(xlFile) = "Boolean" Then
              Exit Sub
          End If
      
          '// Open selected file
          Workbooks.Open xlFile
      End Sub
      

      【讨论】:

      • 我认为这不是问题所在。 “我想提示用户打开一个 Excel 文件在默认文件夹中。”如果我理解正确,他正在寻找一种设置默认文件夹的方法。看看他在代码中的尝试:DefaultFolder = "C:\user\dump"
      • grrr 我想我需要喝咖啡我的错误@vacip
      • 我很高兴你提到了这个方法,我从来没有用过Application.FileDialog(msoFileDialogOpen),所以我下次试试。 :)
      • @vacip 你的好,我喜欢你的回答 +1
      猜你喜欢
      • 2017-09-22
      • 1970-01-01
      • 2017-11-05
      • 2020-05-11
      • 1970-01-01
      • 1970-01-01
      • 2020-05-07
      • 1970-01-01
      • 2021-05-21
      相关资源
      最近更新 更多