You were almost there!
The code you need is as follows:
Sub Get_Data()
FileToOpen = Application.GetOpenFilename _
(Title:="Please choose a file to import", _
FileFilter:="Excel Files *.xls (*.xls),")
''
If FileToOpen = False Then
MsgBox "No file specified.", vbExclamation, "Duh!!!"
Exit Sub
Else
Workbooks.Open Filename:=FileToOpen
End If
End Sub
Using the IF statement ensures that if the user clicks Cancel, your macro won't "fall over"
Hope this helps.
Thanks for the help! One more question if I may...Is there a way for me to have the "Please choose a file to import" dialog open to a specific folder such as C:\Testing ??
This should be what you need...
Sub Get_Data()
ChDrive "C:\"
ChDir "C:\Testing\"
FileToOpen = Application.GetOpenFilename _
(Title:="Please choose a file to import", _
FileFilter:="Excel Files *.xls (*.xls),")
''
If FileToOpen = False Then
MsgBox "No file specified.", vbExclamation, "Duh!!!"
Exit Sub
Else
Workbooks.Open Filename:=FileToOpen
End If
End Sub
NB: The user will still be able to select another directory if they wish, but it will open by default to C:\Testing\ Thanks for the help! One more question if I may...Is there a way for me to have the "Please choose a file to import" dialog open to a specific folder such as C:\Testing ??
Thanks JAF!! I added the ChDrive & ChDir and it works!! This should be what you need... Sub Get_Data()