vba display open dialog box explorer in Excel
Q149. In Excel, how can I get users to select a file for processing using my macro?
For example, my macro takes a file that is selected by user as input, processes the same and provides output file. In Excel, how can I get user to select a file?
To do it in Excel, here is the answer:
- Option Explicit
- Sub ChooseFile()
- Dim blnResponse As Boolean
- ' Show the Open dialog box.
- blnResponse = Application.Dialogs(xlDialogOpen).Show("*.xls")
- ' If blnResponse is False (Open dialog canceled), exit the procedure.
- If blnResponse = False Then
- Exit Sub
- End If
- 'File Processing and Output generation code goes here.
- End Sub
a) Line 5 brings up a dialog box as shown below. User can then select a file and click "Open". Instead of displaying all files use of ("*.xls") filter ensures only Excel files are shown.