VBA EXCEL 提示用户响应选择文件夹并将路径作为字符串变量返回
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26392482/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
VBA EXCEL To Prompt User Response to Select Folder and Return the Path as String Variable
提问by Eric
I am trying to write a VBA code where a dialog box would appear for the user to select where they want to save the files.
However, I just need the path value (e.g. c:\Desktop\Values) returned as a string variable so that I could use it in another function.
Any help would be appreciated.
我正在尝试编写一个 VBA 代码,其中会出现一个对话框,供用户选择他们想要保存文件的位置。但是,我只需要将路径值(例如c:\Desktop\Values)作为字符串变量返回,以便我可以在另一个函数中使用它。任何帮助,将不胜感激。
回答by Gary's Student
Consider:
考虑:
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
This code was adapted from Ozgrid
此代码改编自Ozgrid
and as jkf points out, from Mr Excel
正如 jkf 指出的,来自Excel 先生

