Hello everyone. I am using the file dialog box that allows the user to select a file and have it open. However I am unsure of how to have error trapping in case no file is selected. I just want a basic msg box with "no file selected"? Please see the code below, for what I have so far. Thanks.
Private Sub CommandButton2_Click() ChDrive "C:\" '------------------------------------------------------------------------------------------------------------- 'Import TQ EPMS Valuations file via the file dialog box. '------------------------------------------------------------------------------------------------------------- ChDir "C:\Users\AHernandez1\Desktop\" strThisQuarterFileName = Application.GetOpenFilename _ (Title:="Please choose the file to import for this quarter", _ FileFilter:="Excel Files *.xlsx (*.xlsx),") Workbooks.Open Filename:=strThisQuarterFileName '------------------------------------------------------------------------------------------------------------- End Sub
HI AnthonyWB, try this and it will hopefully do what you wantPrivate Sub CommandButton2_Click() ChDrive "C:\" '------------------------------------------------------------------------------------------------------------- 'Import TQ EPMS Valuations file via the file dialog box. '------------------------------------------------------------------------------------------------------------- ChDir "C:\Users\AHernandez1\Desktop\" strThisQuarterFileName = Application.GetOpenFilename _ (Title:="Please choose the file to import for this quarter", _ FileFilter:="Excel Files *.xlsx (*.xlsx),") If strthisquarterfilename = False Then MsgBox "No file selected" Exit Sub Else Workbooks.Open Filename:=strthisquarterfilename End If'------------------------------------------------------------------------------------------------------------- End Sub
Please leave a message after the beep!
Hello AnthonyWB,
You need to check your variable strThisQuarterFileName before you execute the workbooks open statement. If the user has chosen "Cancel" then the value returned will be either the string "False" or the boolean value False, depending on the variable type. Here is the amended code.
Private Sub CommandButton2_Click() ChDrive "C:\" Dim strThisQuarterFileName As String 'Import TQ EPMS Valuations file via the file dialog box. ChDir "C:\Users\AHernandez1\Desktop\" strThisQuarterFileName = Application.GetOpenFilename _ (Title:="Please choose the file to import for this quarter", _ FileFilter:="Excel Files *.xlsx (*.xlsx),") If strThisQuarterFileName = "False" Then Exit Sub Workbooks.Open Filename:=strThisQuarterFileName End Sub
Sincerely,
Leith Ross
Remember To Do the Following....
1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.2. Thank those who have helped you by clicking the Starbelow the post.
3. Please mark your post [SOLVED] if it has been answered satisfactorily.
Old Scottish Proverb...
Luathaid gu deanamh maille! (Rushing causes delays!)
Thanks the both of you. Hoever neither one your options work. In neither case does the code initiate the msg box. Instead i get run time error 1004 False.xlsx can not be found.
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks