VBA EXCEL提示用户选择文件夹并返回路径作为字符串变量 [英] VBA EXCEL To Prompt User Response to Select Folder and Return the Path as String Variable

查看:488
本文介绍了VBA EXCEL提示用户选择文件夹并返回路径作为字符串变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

推荐答案

考虑:

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

此代码改编自 Ozgrid

并且正如jkf指出,来自 Excel先生

and as jkf points out, from Mr Excel

这篇关于VBA EXCEL提示用户选择文件夹并返回路径作为字符串变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆