+ Reply to Thread
Results 1 to 8 of 8

Help with workbooks.open

Hybrid View

  1. #1
    Registered User
    Join Date
    10-26-2013
    Location
    London, England
    MS-Off Ver
    Excel 2007
    Posts
    5

    Help with workbooks.open

    Hi I am using the vba command
    workbooks.open(filepath & file)
    I get an error message when the specified filepath and file combination do not exist, as you might expect.

    Is there any way to ignore this? The rest of my code is a set of loops to change the file name, following a structured format, so I know that certain file names don't exist whilst others do. I only want it to return data from files which do exist and ignore ones that don't.

    Thanks

  2. #2
    Forum Expert jaslake's Avatar
    Join Date
    02-21-2009
    Location
    Atwood Lake in Mid NE Ohio...look it up.
    MS-Off Ver
    Excel 2010 2019
    Posts
    12,749

    Re: Help with workbooks.open

    Hi ictfc

    Welcome to the Forum!!!

    There's several ways to handle this:
    Sub TestForFileExistence_1()
    On Error Resume Next
    
       'Your Code
       
       On Error GoTo 0
    End Sub
    Or:
    Sub TestForFileExistence_2()
    On Error GoTo SkipMe
    
       'Your Code
       
    SkipMe:
       'More Code
    End Sub
    Or:
    Sub TestForFileExistence_3()
        Const sPath   As String = "C:\myPath\"    ' note trailing "\"
        Dim vsFile    As Variant
    
        For Each vsFile In Array("F1.xls", "F2.xls", "F3.xls")
            If Len(Dir(vsFile)) Then
                With Workbooks.Open(Filename:=sPath & vsFile)
    
                    '  code here
    
                    .Close SaveChanges:=True
                    Name sPath & vsFile As sPath & Format(Date, "yyyymmdd_") & vsFile
                End With
            End If
        Next vsFile
    End Sub
    And my personal preference...see this link...
    http://www.excelguru.ca/content.php?157
    John

    If you have issues with Code I've provided, I appreciate your feedback.

    In the event Code provided resolves your issue, please mark your Thread as SOLVED.

    If you're satisfied by any members response to your issue please use the star icon at the lower left of their post.

  3. #3
    Forum Expert
    Join Date
    07-31-2010
    Location
    California
    MS-Off Ver
    Excel 2007
    Posts
    4,070

    Re: Help with workbooks.open

    The quick solution is like this

    On Error Resume Next
    Workbooks.Open (filepath & file)
    On Error GoTo 0

  4. #4
    Forum Expert jaslake's Avatar
    Join Date
    02-21-2009
    Location
    Atwood Lake in Mid NE Ohio...look it up.
    MS-Off Ver
    Excel 2010 2019
    Posts
    12,749

    Re: Help with workbooks.open

    Hi ictfc

    At a minimum post your entire revised Code...a Sample File might be helpful...post your entire revised Code first.

  5. #5
    Registered User
    Join Date
    10-26-2013
    Location
    London, England
    MS-Off Ver
    Excel 2007
    Posts
    5

    Re: Help with workbooks.open

    Hi, thanks for the replies. I tried both approaches my code now skips the first file it can not find but for the next file it still gives me an error.

  6. #6
    Registered User
    Join Date
    10-26-2013
    Location
    London, England
    MS-Off Ver
    Excel 2007
    Posts
    5

    Re: Help with workbooks.open

    Here is my complete code
    Filepath = ("filepath on my computer\")
     n = ws.Range("car").Count
     m = ws.Range("type").Count
     p = ws.Range("reg").Count
    
     j = 1
     i = 1
     k = 1
    
     For j = 1 To n 
    
     For i = 1 To m 
    
     For k = 1 To p 
    
     cr = ws.Range("car").Cells(j, 1)
     ty = ws.Range("type").Cells(i, 1)
     rg = ws.Range("reg").Cells(k, 1)
    
     file2 = ("\ABC-123-" & rg & "-" & ty & "-" & cr & "-CHECK.xlsm")
    
    On Error GoTo Skipme
    
    
     Workbooks.Open (Filepath & file2)
    
     ActiveWorkbook.Worksheets("CarInfo").Visible = True
     ActiveWorkbook.Worksheets("CarInfo").Select
     ActiveWorkbook.Worksheets("CarInfo").Range("F1:F87").Select
    
     Application.CommandBars.FindControl(ID:=398).Execute
     Application.CommandBars.FindControl(ID:=398).Execute
    
     Selection.Copy
    
     ActiveWorkbook.Close (False)
    
     ActiveSheet.Cells(1 + (i + (n + 2) * (j - 1)), 2).Select
    
     Selection.PasteSpecial
     clipboard.Clear
    
    skipme:
    
    
     Next k
    
     clipboard.Clear
    
     Next i
    
     clipboard.Clear
    
     Next j
    
     End Sub

  7. #7
    Forum Expert jaslake's Avatar
    Join Date
    02-21-2009
    Location
    Atwood Lake in Mid NE Ohio...look it up.
    MS-Off Ver
    Excel 2010 2019
    Posts
    12,749

    Re: Help with workbooks.open

    Hi ictfc

    I'd suggest this is not your ACTUAL COMPLETE Code.

  8. #8
    Forum Expert jaslake's Avatar
    Join Date
    02-21-2009
    Location
    Atwood Lake in Mid NE Ohio...look it up.
    MS-Off Ver
    Excel 2010 2019
    Posts
    12,749

    Re: Help with workbooks.open

    Hi ictfc

    This Code compiles and runs...have no clue it it does as you require.
    Sub test()
       Filepath = ("filepath on my computer\")
       Set ws = Sheets("Sheet1")
       n = ws.Range("car").Count
       m = ws.Range("type").Count
       p = ws.Range("reg").Count
    
       j = 1
       i = 1
       k = 1
    
       For j = 1 To n
          For i = 1 To m
             For k = 1 To p
                cr = ws.Range("car").Cells(j, 1)
                ty = ws.Range("type").Cells(i, 1)
                rg = ws.Range("reg").Cells(k, 1)
    
                file2 = ("\ABC-123-" & rg & "-" & ty & "-" & cr & "-CHECK.xlsm")
    
                If FileFolderExists(Filepath & file2) Then
                   Workbooks.Open (Filepath & file2)
    
                   ActiveWorkbook.Worksheets("CarInfo").Visible = True
                   ActiveWorkbook.Worksheets("CarInfo").Select
                   ActiveWorkbook.Worksheets("CarInfo").Range("F1:F87").Select
    
                   Application.CommandBars.FindControl(ID:=398).Execute
                   Application.CommandBars.FindControl(ID:=398).Execute
    
                   Selection.Copy
    
                   ActiveWorkbook.Close (False)
    
                   ActiveSheet.Cells(1 + (i + (n + 2) * (j - 1)), 2).Select
    
                   Selection.PasteSpecial
                   Application.CutCopyMode = False
    
                End If
             Next k
             Application.CutCopyMode = False
    
          Next i
          Application.CutCopyMode = False
    
       Next j
    
    End Sub
    
    
    Public Function FileFolderExists(strFullPath As String) As Boolean
    'Author       : Ken Puls (www.excelguru.ca [1])
    'Macro Purpose: Check if a file or folder exists
    
        On Error GoTo EarlyExit
        If Not Dir(strFullPath, vbDirectory) = vbNullString Then FileFolderExists = True
        
    EarlyExit:
        On Error GoTo 0
    
    End Function

+ 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] Open workbooks update the main one and close the other workbooks
    By stojko89 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 07-18-2013, 07:12 AM
  2. [SOLVED] Open Multiple Workbooks, Record names, Copy paste to Active Workbook, Close the Workbooks
    By vba_madness in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 01-24-2013, 06:09 AM
  3. Replies: 14
    Last Post: 08-17-2012, 10:54 AM
  4. Replies: 1
    Last Post: 01-02-2006, 11:30 PM
  5. Replies: 0
    Last Post: 12-30-2005, 04:35 AM

Tags for this Thread

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