Saturday, April 20, 2019

Opening Files Using File Dialog Box in Excel VBA

Opening Files Using File Dialog Box in Excel VBA

Solution: You can get the file name using file dialog and open it by using Workbooks.Open method


Sub OpenWorkbookUsingFileDialog()

Dim fdl As FileDialog
Dim FileName As String
Dim FileChosen As Integer

Set fdl = Application.FileDialog(msoFileDialogFilePicker)

'Set the caption of the dialog box,
fdl.Title = "Please Select a Excel Macro File"
'Set the InitialFile Path
fdl.InitialFileName = "c:\"
'Set the Folder View
fdl.InitialView = msoFileDialogViewSmallIcons
'Set the filter
fdl.Filters.Clear
fdl.Filters.Add "Excel Macros Files", "*.xlsm"

FileChosen = fdl.Show

If FileChosen <> -1 Then
'Not choosen anything / Clicked on CANCEL
MsgBox "You have choosen nothing"
Else
'display name and complete path of file chosen
MsgBox fdl .SelectedItems(1)
End If

FileName = fdl.SelectedItems(1)
'Open the File
Workbooks.Open (FileName)

End Sub

No comments:

Post a Comment