VBA Select Folder

VBA select folder relies upon the Microsoft Shell Controls And Automation object library. You will need to set a Reference to it. In the VBA Editor's Tools menu, click References... scroll down to "Microsoft Shell Controls And Automation" and choose it. We have to add it to be able to use the Shell32 library.

Sub FolderSelection()
Dim MyPath As String
MyPath = SelectFolder("Select Folder", "")
If Len(MyPath) Then
MsgBox MyPath
Else
MsgBox "Cancel was pressed"
End If
End Sub

'Both arguements are optional. The first is the dialog caption and
'the second is is to specify the top-most visible folder in the
'hierarchy. The default is "My Computer."

Function SelectFolder(Optional Title As String, Optional TopFolder _
As String) As String
Dim objShell As New Shell32.Shell
Dim objFolder As Shell32.Folder

'If you use 16384 instead of 1 on the next line,
'files are also displayed
Set objFolder = objShell.BrowseForFolder _
(0, Title, 1, TopFolder)
If Not objFolder Is Nothing Then
SelectFolder = objFolder.Items.Item.Path
End If
End Function
  
This site is powered by Site Build It!. If you enjoy it, please check out the
Site Build It homepage
 to learn more and on how to build a success-guaranteed site with no technical skills.





Return from VBA Select Folder to VBA Code Samples

Return to our Homepage