VBA Excel获取文件路径(以文件夹结尾) [英] VBA Excel Getting File Path (ends with folder)
问题描述
从上一个问题我知道如何让用户点击浏览器按钮并浏览到他们可能想要打开的特定文件。
From a previous question I know how to go about letting the user click on a "browser" button and navigate to a specific file that they might want to open.
代码
Private Sub CommandButton2_Click()
Dim vaFiles As Variant
vaFiles = Application.GetOpenFilename()
ActiveSheet.Range("B9") = vaFiles
End Sub
我想创建一个第二个浏览器按钮,让用户浏览到一个文件夹。该文件夹将保存我的程序创建的 .pdf
文件。以下是问题: GetOpenFilename
要求用户点击文件。如果文件夹中没有文件,那么用户无法做到这一点。
I want to create a second browser button that will let the user navigate to a folder. This folder is going to be where they save the .pdf
file that my program creates. Here's the problem: The GetOpenFilename
requires the user to click on a file. If there's no file in the folder then there's nothing the user can do.
我希望这样很清楚...
I hope that was clear enough...
谢谢
推荐答案
使用Application.FileDialog对象
Use the Application.FileDialog object
Sub SelectFolder()
Dim diaFolder As FileDialog
' Open the file dialog
Set diaFolder = Application.FileDialog(msoFileDialogFolderPicker)
diaFolder.AllowMultiSelect = False
diaFolder.Show
MsgBox diaFolder.SelectedItems(1)
Set diaFolder = Nothing
End Sub
这篇关于VBA Excel获取文件路径(以文件夹结尾)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!