Quick Tips: VBA, Make a file dialog open

Here’s the code to make a file dialog to open up. Once the user has selected a file, it will be saved in the fileName variable, and you can use that variable to open up the file contents.

In order for this code to work, you must activate the Microsoft Office 14.0 Object Library, or else your VBA will result in an error.  This VBA should work in Microsoft Access and Microsoft Excel.

Dim fileName As String
 
Application.FileDialog(msoFileDialogOpen).Show
fileName = Application.FileDialog(msoFileDialogOpen).SelectedItems(1)