[英]Folder Explorer is Not Work well in Excel VBA
Private Sub ButtonPath_Click()
Cells(1, 32).Value = FolderExplorer()
End Sub
上面是我的用户窗体的代码。 下面的代码是我的模块的FolderExplorer()函数。
'//Source : http://software-solutions-online.com/vba-folder-dialog/ , edited by me.
Public Function FolderExplorer()
Dim intResult As Integer
Dim strPath As String
Application.FileDialog(msoFileDialogFolderPicker).ButtonName _
= "Select Path"
'the dialog is displayed to the user
intResult = Application.FileDialog(msoFileDialogFolderPicker).Show
'checks if user has cancled the dialog.
If intResult <> 0 Then
'dispaly message box
'Call MsgBox(Application.FileDialog(msoFileDialogFolderPicker _
).SelectedItems(1), _
vbInformation, "Selected Folder")
FolderExplorer() = Application.FileDialog(msoFileDialogFolderPicker).SelectedItems(1)
End If
End Function
起初看起来效果不错。 当我按用户窗体中的按钮时,FolderExplorer弹出。 但是当我选择文件夹路径时,再次弹出文件夹浏览器,而当我选择文件夹路径时,再次弹出文件夹浏览器...。
我的代码中没有循环。 我怎么解决这个问题?
感谢您的提前答复。
您正在FolderExplorer() = Application.FileDialog(msoFileDialogFolderPicker).SelectedItems(1)
递归调用FolderExplorer()
FolderExplorer() = Application.FileDialog(msoFileDialogFolderPicker).SelectedItems(1)
。
您应该使用FolderExplorer = Application.FileDialog(msoFileDialogFolderPicker).SelectedItems(1)
返回该值。
以下是您的代码的简化版本。
Public Function FolderExplorer()
Dim intResult As Integer
Dim strPath As String
With Application.FileDialog(msoFileDialogFolderPicker)
.ButtonName = "Select Path"
intResult = .Show
If intResult <> 0 Then
FolderExplorer = .SelectedItems(1)
End If
End With
End Function
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.