Home : Computing : VBA

GetFolder

VBA code to prompt user for a folder.
Function GetFolder(strPath As String) As String
    Dim fldr As FileDialog
    Dim sItem As String
    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:
    GetFolder = sItem
    Set fldr = Nothing
End Function
You call it by using something like:
Private Sub cmdSetOutputDir_Click()
    Dim InitialFolder As String, NewFolder
    InitialFolder = GetSetting(APP_NAME, "Settings", "ExportPath", "M:\Finance\Invoices")
    NewFolder = GetFolder(InitialFolder)
    If Len(NewFolder) = 0 Then NewFolder = InitialFolder
    SaveSetting APP_NAME, "Settings", "ExportPath", NewFolder
    lblDir = NewFolder
End Sub

Links to other sites

MrExcel - forum thread from which this source code was obtained.
Author:  Mark Carter
Created: 09-Dec-2008
Updated: 09-Dec-2008