1. First create a userform with a textbox and a command button:
2. Then enter the following code in the commandbutton click event:
Sub CommandButton1_Click()
Dim fd As Office.FileDialog
Set fd = Application.FileDialog(msoFileDialogFilePicker)
With fd
.InitialFileName = ThisWorkbook.Path
.AllowMultiSelect = False
.Title = "Please select the file."
.Filters.Clear
.Filters.Add "All Files", "*.*"
' Show the dialog box. If the .Show method returns True, the user picked at least one file. If the .Show method returns False, the user clicked Cancel.
If .Show = True Then
UserForm1.TextBox1.Value = .SelectedItems(1) 'replace txtFileName with your textbox
End If
End With
End Sub
3. Launch your userform by entering following macro in new module for example:
>> As a result, you will have a user interface where you can pick a file in your folder and get the file path in the textbox for future usage:
Created by DoMyExcel.com © | Excel & VBA Consulting | Customized Excel Solutions starting at 89 USD