+ Reply to Thread
Results 1 to 3 of 3

Skip a 'file not found' dialogue in a loop

  1. #1
    Registered User
    Join Date
    11-27-2012
    Location
    Toronto
    MS-Off Ver
    Excel 2010
    Posts
    15

    Skip a 'file not found' dialogue in a loop

    Hi,

    I am having a problem with a portion of a macro which is designed to loop through the file names of several files and open them all. The way it works is that you would set a start date and end date for the files the macro should open, then run. However, if one of the files happens to be missing, the macro will stop working. For example, I am looping through files from December 24th to December 31st, but there is no file for December 24th.

    I want to avoid requiring the user to indicate dates for which the files are missing, also I have tried a GoTo statement for error handling, however the macro not finding the file does not seem to be considered an error. What is a workaround that would automatically skip one step in the loop when it doesn't encounter a file with the name that it is trying to open? My code is below.

    Much Appreciated,

    T

    PHP Code: 
    Public MainPathFldrPWSourceThisFile As String
    Public NumDaysNow As Long
    Sub LoopTest
    ()
    '
    LoopTest Macro
    '

    '
        
    MainPath Range("A2")
    MacPath Range("A3")
    Fldr Range("A6")
    PWSource Range("A9")
    ThisFile Range("F2")
    NumDays Range("F11")
    Now Range("F12")
        
    'Open the first sheet

        ChDir _
            MainPath & Fldr
        
        Workbooks.Open Filename:= _
            MainPath & Fldr & PWSource
        
    '
    set the value of check variable to 1 to start
        
        Windows
    (ThisFile).Activate
        
        Range
    ("F12") = Now 1
        Now 
    Range("F12")
       
    'loop through sheets and combine the data into one
        
        For Now = 1 To NumDays
       
        Windows(ThisFile).Activate

        MainPath = Range("A2")
        Fldr = Range("A6")
        PWSource = Range("A9")
      
        Workbooks.Open Filename:= _
            MainPath & Fldr & PWSource
           
    '
    add 1 to check variable

        Windows
    (ThisFile).Activate
        Range
    ("F12") = Now 1

    Next

    'reset check variable
        
        '
    Windows(ThisFile).Activate
        
    'Range("F12") = 0
        '
    Now Range("F12")

    End Sub 

  2. #2
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,646
    You can check if a file exists using Dir.

    PS Now is a VBA statement and shouldn't be used for the name of a variable.
    If posting code please use code tags, see here.

  3. #3
    Registered User
    Join Date
    11-27-2012
    Location
    Toronto
    MS-Off Ver
    Excel 2010
    Posts
    15

    Re: Skip a 'file not found' dialogue in a loop

    That's awesome! My code is a lot shorter and does exactly what I needed by combining an if statement with dir.

    Thanks!

    PHP Code: 
    Public MainPathFldrPWSourceThisFile As String
    Public NumDaysBegin As Long
    Public CheckDate As Date
    Sub LoopTest
    ()
    '
    LoopTest Macro
    '

    '
        
    MacPath Range("A3")
    ThisFile Range("F2")
    NumDays Range("F11")
    Begin Range("F12")
    CheckDate Range("E14")
        
        
    Application.DisplayAlerts False

    'loop through books and open
        
        For Begin = 0 To NumDays
        
        MainPath = Range("A2")
        Fldr = Range("A6")
        PWSource = Range("A9")
        CheckDate = Range("E14")

    '
    check if file exists

    If Len(Dir(MainPath Fldr PWSource)) <> 0 Then
        
        Workbooks
    .Open Filename:= _
            MainPath 
    Fldr PWSource
       
        Windows
    (ThisFile).Activate

        Range
    ("F12") = Begin 1
        
    Else

        
    Range("F12") = Begin 1

    End 
    If

    Next

    'reset check variable
        
        Windows(ThisFile).Activate
        Range("F12") = 0
        Begin = Range("F12")

    End Sub 

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