VBA Macro to Ask User to Browse for Excel File to Open

ekfasy

New Member
Joined
May 16, 2016
Messages
11
Hello,

I'm trying to write vba code that will ask the user to browse for a certain excel spreadsheet on their computer, then open that file. Is this possible?

I currently have it set up to ask the user to input the file name to open it, but that requires the user to make no errors in their typing...which is not reliable.

Any suggestions would be much appreciated.

Thanks in advance!
 
Application.GetOpenFilename retutns an array only if MultiSelect argument is set to True.

The type mismatch in this case can be avoided by replacing False with "False".
Rich (BB code):
If TargetFile = "False" Then
So basically this?

Rich (BB code):
Sub OpenFileDialog()
' Defines variables
Dim TargetFile As String


' Open dialogue box to browse for file
TargetFile = Application.GetOpenFilename _
(Title:="Please choose a file to open", _
FileFilter:="Excel Files *.xls* (*.xls*),")


' If no file is selected then...
If TargetFile = "False" Then
    ' Display message box with an error
    MsgBox "No file selected.", vbExclamation, "Sorry!"
    ' Exit the macro
    Exit Sub
' Else if a valid file is selected then...
Else
    ' Open the selected workbook
    Workbooks.Open FileName:=TargetFile
End If


End Sub
 
Upvote 0

Excel Facts

Did you know Excel offers Filter by Selection?
Add the AutoFilter icon to the Quick Access Toolbar. Select a cell containing Apple, click AutoFilter, and you will get all rows with Apple
Fishboy

Yes, though declaring TargetFile as Variant would also work.
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,337
Members
452,637
Latest member
Ezio2866

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top