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

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

问题描述

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

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:DesktopValues) 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

正如 jkf 指出的,来自 Excel 先生

and as jkf points out, from Mr Excel

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

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