Browse File Dialog in Excel VBA
In Excel file, if you are doing any folder operations or trying to read content from another text,xlsx, csv file. It is better to use this file dialog box rather than manually typing the file name with path.
It would reduce the typing errors and also the file path will be exact as how it is in the system.
Lets see how to invoke it from a VBA code and get the file path directly.
Sub Choose_File() Dim sSh As Worksheet Dim filedialog As Office.FileDialog, txtFileName Set filedialog = Application.FileDialog(msoFileDialogFilePicker) Set sSh = ThisWorkbook.Sheets("Settings") txtFileName = "" With filedialog .AllowMultiSelect = False .Title = "Choose File(s)" .Filters.Add "Excel", "*.xls*" .Filters.Add "Csv", "*.csv*" .Filters.Add "Text", "*.txt*" .Filters.Add "All", "*.*" If .Show = True Then txtFileName = .SelectedItems(1) End If End With sSh.Cells(1, 2) = txtFileName sSh.Columns("B:B").AutoFit End Sub
Rename the sheet name from “Settings” to any worksheet name as it is in your Excel workbook.