Paul's Programming Notes     Archive     Feed     Github

Get Folder Path From User Input


This is some very useful code from Stack Overflow which allows the user to select a folder in VBA and reads the folder path into a variable. I'm currently making a macro which opens several XML files in a folder, and this will help users to input the folder where those XML files are located:

Sub SelectFolder()
    Dim diaFolder As FileDialog

    ' Open the file dialog
    Set diaFolder = Application.FileDialog(msoFileDialogFolderPicker)
    diaFolder.AllowMultiSelect = False
    diaFolder.Show

    MsgBox diaFolder.SelectedItems(1)

    Set diaFolder = Nothing
End Sub