Browse for a folder in VBA

DHS100

Board Regular
Joined
May 21, 2006
Messages
149
Hi,

I'm having a bit of trouble getting this to work. All I want is for a dialogue box to pop up so the user can select a folder and to be able to default to the last folder they visited. I've seen lots of ways to do this but there is a problem with the best method I've found so far. I'm using this:

Set ShellApp = CreateObject("Shell.Application").BrowseForFolder(0, "Please choose a folder", 0, "C:\test\")

The problem is that there is no way to go up a level in the directory structure. The above code mean you couldn't go to "C:\". Any help with this would be very much appreciated.

Thanks
 
Hi Richard,

I really appreciate your efforts on this, and those of anyone else who looks into it.

This isn't really a showstopper for the typical use of the program - I only discovered it because I was creating temporary folders for testing and found I couldn't delete them immediately.

That being said, my app will be used by many people at my company, so I'd like it to work as well as possible. I'll use your ChDir "C:\" workaround unless another solution comes along.

BTW the best thing about this possible bug is that I discovered this site and community. I've always tried to figure things out on my own and been reluctant to post questions on help sites. Since I joined the site a few days ago, I've really enjoying learning from others and sharing what I know.

Thanks again,
-Jerry
 
Upvote 0

Excel Facts

What did Pito Salas invent?
Pito Salas, working for Lotus, popularized what would become to be pivot tables. It was released as Lotus Improv in 1989.
A "better" way of handling the locked folder issue is to grab the initial folder at the start of the function and reset at the end. The problem is caused by FileDialog changing the current directory upon clicking ok. Try the following amended code.

Function GetFolder(strPath As String) As String
Dim fldr As FileDialog
Dim sItem As String
Dim sInitDir as String
sInitDir = CurDir ' Store initial directory
Set fldr = Application.FileDialog(msoFileDialogFolderPicker)
With fldr
.Title = "Select a Folder"
.AllowMultiSelect = False
.InitialFileName = strPath
If .Show <> -1 Then GoTo NextCode
sItem = .SelectedItems(1)
End With
NextCode:
' Reset directory before exit
ChDrive sInitDir ' Return to the Initial Drive
ChDir sInitDir ' Resets directory for Initial Drive
GetFolder = sItem
Set fldr = Nothing
End Function
 
Upvote 0
The previous codes had some problems in 64-bit Excel 2010. Here is the solution:

Function GetFolder(InitDir As String) As String
Dim fldr As FileDialog
Dim sItem As String
sItem = InitDir
Set fldr = Application.FileDialog(msoFileDialogFolderPicker)
With fldr
.Title = "Select a Folder"
.AllowMultiSelect = False
If Right(sItem, 1) <> "\" Then
sItem = sItem & "\"
End If
.InitialFileName = sItem
If .Show <> -1 Then
sItem = InitDir
Else
sItem = .SelectedItems(1)
End If
End With
GetFolder = sItem
Set fldr = Nothing
End Function
 
Last edited:
Upvote 0
Hi,

Can someone tell me how I can open the folder picker and then once selection the folder it will store the folder path in a cell of my document?

Thanks
 
Upvote 0
Do I need to enable any libraries?
The dialog never comes up, and it treats fldr as a string
Code:
Set fldrs = Application.FileDialog(msoFileDialogFolderPicker)
ACTUALLY makes fldr say "Application.FileDialog(msoFileDialogFolderPicker)"

The dialog never comes up.
 
Upvote 0
Hi,

Can someone tell me how I can open the folder picker and then once selection the folder it will store the folder path in a cell of my document?

Thanks

Hi

This is my first post I hope its helpful.

Insert this code at the end. It will set the value of B1 in sheet1 as the selected folder. I tested in XL2007 (x64)

Code:
    Sheet1.Cells(1, 2).Value = BrowseFolder.SelectedItems(1)
 
Upvote 0
Do I need to enable any libraries?
The dialog never comes up, and it treats fldr as a string
Code:
Set fldrs = Application.FileDialog(msoFileDialogFolderPicker)
ACTUALLY makes fldr say "Application.FileDialog(msoFileDialogFolderPicker)"

The dialog never comes up.

Hi

This line of code just sets 'fldrs' as application.FileDialog(msoFileDialogFolderPicker)
You need to declare fldrs as a FileDialog variable.
Code:
dim fldrs as FileDialog

To bring up the dialog you need to use.
Code:
fldrs.show


The chosen folder will be returned as fldrs.SelectedItems(1).
 
Last edited:
Upvote 0
As a follow-up question, how is it possible to utilize the .allowmultiselect?

I set this variable equal to true, but it seems that the functionality isn't available.

Edit: I'm holding the Ctrl button while multi-selecting.
 
Upvote 0

Forum statistics

Threads
1,224,814
Messages
6,181,121
Members
453,021
Latest member
Justyna P

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top