如何让我的浏览对话框转到VBA中的特定文件夹?

时间:2014-07-16 15:28:40

标签: excel vba

我在VBA中有一个宏,当我点击浏览按钮获取文件夹位置时,它总是预先加载我的文档文件夹。如何自动预加载到存储excel文件的文件夹,以便所有用户都可以单击确定?

例如:

  

用户想要加载文件夹。他点击了浏览按钮。该   弹出用于选择文件夹的对话框。它已经在文件夹中了   存储excel文件的位置。然后用户单击确定以加载   文件夹中。

我有这个代码用对话框加载文件夹:

Function GetFolder() As String
    Dim fldr As FileDialog
    Dim sItem As String
    Set fldr = Application.FileDialog(msoFileDialogFolderPicker)
    With fldr
       .Title = "Select A Folder Containing Your EM Traces"
       .AllowMultiSelect = False
       If .Show <> -1 Then GoTo NextCode
       sItem = .SelectedItems(1)
    End With
NextCode:
    GetFolder = sItem
    Set fldr = Nothing
End Function

感谢您的帮助!

1 个答案:

答案 0 :(得分:0)

您可以使用InitialFileName对象的FileDialog属性。例如,使用您的代码:

Function GetFolder() As String
    Dim fldr As FileDialog
    Dim sItem As String
    Set fldr = Application.FileDialog(msoFileDialogFolderPicker)
    With fldr
       .Title = "Select A Folder Containing Your EM Traces"
       .AllowMultiSelect = False
       .InitialFileName = "C:\Some Folder" 'Set the directory here
       If .Show <> -1 Then GoTo NextCode
       sItem = .SelectedItems(1)
    End With
NextCode:
    GetFolder = sItem
    Set fldr = Nothing
End Function