Tuesday, March 15, 2016

How to create a file picker interface in EXCEL VBA with browse button in 3 steps

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:



Sub launch_userform1()
userform1.show
End Sub

>> 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
Services provided to Individuals and Businesses | Free Quote: DoMyExcel@gmail.com

No comments:

Post a Comment