Select the Single File

  • FileDialog -- is a property of APPLICATION Object
  • FileDialog consists of 4 members
  •  

     

  • As i am going to select a file here i am using MSOFileDIALOGFILEPICKER option
  •  

    Click on below mentioned image to watch video:

    Donwload the Workbook

     

  • Code to select the Single file
  •  

    Sub Select_Workbook_Using_FilePicker_Of_FileDialog()

    'Declare a variable for FileDialog
    Dim FD As FileDialog
    Set FD = Application.FileDialog(msoFileDialogFilePicker)

    'Using With Block
    With FD
    'Provide the Title to the Filedialog
    .Title = "Select The Workbook"

    'To Select Single File
    .AllowMultiSelect = False

    'Filter the Files exists in the folder
    .Filters.Add "Excel", "*.xlsx", 1

    'Open the Workbook
    If .Show = -1 Then
    MsgBox .SelectedItems(1)
    Workbooks.Open (.SelectedItems(1))
    Else:
    MsgBox "Not Selected The File"
    End If
    End With

    End Sub

     

     

  • Code to select the Multiple Files
  •  

    Sub Select_Workbooks_Using_FilePicker_Of_FileDialog()
    'Declare a variable for FileDialog
    Dim FD As FileDialog
    Set FD = Application.FileDialog(msoFileDialogFilePicker)

    'Using With Block
    With FD
    'Provide the Title to the Filedialog
    .Title = "Select The Workbook"

    'To Select Multiple files
    .AllowMultiSelect = True

    'Filter the Files exists in the folder
    .Filters.Add "Excel", "*.xlsx", 1

    'Open the Workbooks
    If .Show = -1 Then
    Dim Wkb As Variant
    For Each Wkb In FD.SelectedItems
    Workbooks.Open (Wkb)
    Next
    Else:
    MsgBox "Not Selected The File"
    End If
    End With

    End Sub