+ Reply to Thread
Results 1 to 2 of 2

Exit macro when no file is selected for open

Hybrid View

fblaze Exit macro when no file is... 12-10-2014, 01:38 PM
protonLeah Re: Exit macro when no file... 12-10-2014, 04:08 PM
  1. #1
    Registered User
    Join Date
    02-26-2013
    Location
    India
    MS-Off Ver
    Excel 2007
    Posts
    12

    Exit macro when no file is selected for open

    I need to exit the macro when if the user is not selecting any file and clicks "Cancel" in the file picker dialog box. Below is my exisintg code

    Sub OpenFile()
    
    With Application.FileDialog(msoFileDialogFilePicker)
        .InitialFileName = ThisWorkbook.Path
        
        If .Show <> -1 Then Exit Sub
        strFile = .SelectedItems(1)
        Sheets("Sheet1").Cells.ClearContents
        Open strFile For Input As #1
        i = 1
        Do While Not EOF(1) ' Loop until end of file.
            Line Input #1, Text
            Sheets("Sheet1").Range("A" & i) = Text
            i = i + 1
        Loop
        Close #1
    End With
    
    End Sub
    
    Sub Opertion()
    
    OpenFile
    
    End Sub()

  2. #2
    Forum Guru
    Join Date
    03-02-2006
    Location
    Los Angeles, Ca
    MS-Off Ver
    WinXP/MSO2007;Win10/MSO2016
    Posts
    12,963

    Re: Exit macro when no file is selected for open

        If .Show <> -1 Then Exit Sub
        if .SelectedItems.Count = 0 Then Exit Sub
    Ben Van Johnson

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. [SOLVED] Excel Macro: How to open a file with selected path?
    By polimer in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 04-02-2012, 02:29 PM
  2. Command line for open excel, insert a sheet, execute a macro, save file exit?
    By uschxc in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 03-26-2012, 12:28 PM
  3. Macro to open a selected file and extract info
    By rirons04 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 01-12-2012, 08:18 PM
  4. How to open a text file in a selected sheet?
    By mithil in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 10-28-2010, 03:51 PM
  5. Open pdf file selected from excel
    By davesexcel in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 09-23-2008, 09:23 AM

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.6.0 RC 1