VBA EXCEL提示用户响应选择文件夹并将路径作为字符串变量返回

Eri*_*ric 11 excel vba excel-vba

我正在尝试编写一个VBA代码,其中会出现一个对话框供用户选择要保存文件的位置.但是,我只需要将路径值(例如c:\Desktop\Values)作为字符串变量返回,以便我可以在另一个函数中使用它.任何帮助,将不胜感激.

Gar*_*ent 34

考虑:

Function GetFolder() As String
    Dim fldr As FileDialog
    Dim sItem As String
    Set fldr = Application.FileDialog(msoFileDialogFolderPicker)
    With fldr
        .Title = "Select a Folder"
        .AllowMultiSelect = False
        .InitialFileName = Application.DefaultFilePath
        If .Show <> -1 Then GoTo NextCode
        sItem = .SelectedItems(1)
    End With
NextCode:
    GetFolder = sItem
    Set fldr = Nothing
End Function
Run Code Online (Sandbox Code Playgroud)

此代码改编自Ozgrid

正如jkf从Excel先生那里指出的那样