|
|
|
|
|
|
Excel
|
Browse For Folder
|
|
Ease of Use
|
Easy
|
Version tested with
|
97, 2003
|
Submitted by:
|
Ken Puls
|
Description:
|
This function will allow a user to Browse for a folder (at a user defined starting point if desired.) It is easy to use, as the code requires no API's or special references.
|
Discussion:
|
There may be times when a user needs to point a procedure to a particular folder, say to open of save a file from/to. This macro allows the user to browse to the folder, and returns the full file path to the folder selected, or FALSE if an invalid entry was chosen.
Another approach to this can (by DRJ) be found at http://www.vbaexpress.com/kb/getarticle.php?kb_id=246
|
Code:
|
instructions for use
|
Option Explicit
Function BrowseForFolder(Optional OpenAt As Variant) As Variant
Dim ShellApp As Object
Set ShellApp = CreateObject("Shell.Application"). _
BrowseForFolder(0, "Please choose a folder", 0, OpenAt)
On Error Resume Next
BrowseForFolder = ShellApp.self.Path
On Error GoTo 0
Set ShellApp = Nothing
Select Case Mid(BrowseForFolder, 2, 1)
Case Is = ":"
If Left(BrowseForFolder, 1) = ":" Then GoTo Invalid
Case Is = "\"
If Not Left(BrowseForFolder, 1) = "\" Then GoTo Invalid
Case Else
GoTo Invalid
End Select
Exit Function
Invalid:
BrowseForFolder = False
End Function
|
How to use:
|
- Copy above code.
- In Excel press Alt + F11 to enter the VBE.
- Press Ctrl + R to show the Project Explorer.
- Right-click desired file on left (in bold).
- Choose Insert -> Module.
- Paste code into the right pane.
- Press Alt + Q to close the VBE.
- Save workbook before any other changes.
|
Test the code:
|
- In Excel press Alt + F11 to enter the VBE.
- Press Ctrl + G to show the Immediate window
- To test with no default, type the following, then press Enter: debug.print browseforfolder
- You should be prompted with the browse window. Upon completion, it should type the name of the folder you selected in the Immediate window under what you typed above.
- To test with a default, type the following, then press Enter: debug.print browseforfolder("C:\temp")
- You should be prompted with the browse window already in the "Temp" folder. Upon completion, it should type the name of the folder you selected in the Immediate window under what you typed above.
|
Sample File:
|
BrowseForFolder.zip 10.25KB
|
Approved by mdmackillop
|
This entry has been viewed 2694 times.
|
|