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弹出。但是当我选择文件夹路径时,Folder Explorer再次弹出,而当我选择文件夹路径时,Folder Explorer再次弹出....永恒。
我的代码中没有循环。我怎么解决这个问题?
感谢您的提前答复。
您正在中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
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句