+ Reply to Thread
Results 1 to 4 of 4

Thread: Error Trapping File Dialog Box

  1. #1
    Valued Forum Contributor
    Join Date
    03-30-2010
    Location
    Washington DC
    MS-Off Ver
    Excel 2007
    Posts
    458

    Error Trapping File Dialog Box

    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

  2. #2
    Forum Guru Mordred's Avatar
    Join Date
    07-06-2010
    Location
    Winnipeg, Canada
    MS-Off Ver
    2007, 2010
    Posts
    2,276

    Re: Error Trapping File Dialog Box

    HI AnthonyWB, try this and it will hopefully do what you want
    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),")
    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!

  3. #3
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & read 2007
    Posts
    15,979

    Re: Error Trapping File Dialog Box

    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 Star below the post.
    3. Please mark your post [SOLVED] if it has been answered satisfactorily.


    Old Scottish Proverb...
    Luathaid gu deanamh maille! (Rushing causes delays!)

  4. #4
    Valued Forum Contributor
    Join Date
    03-30-2010
    Location
    Washington DC
    MS-Off Ver
    Excel 2007
    Posts
    458

    Re: Error Trapping File Dialog Box

    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.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.2.0