+ Reply to Thread
Results 1 to 8 of 8

VBA Code - Run-time Error 1004 can't find file

Hybrid View

  1. #1
    Registered User
    Join Date
    12-26-2017
    Location
    Portland, OR, USA
    MS-Off Ver
    2016
    Posts
    4

    VBA Code - Run-time Error 1004 can't find file

    Hello,

    I am VERY new to VBA and I am getting a strange error when I run my macro. The code is below and the goal is to combine "Sheet1" of several workbooks into a new workbook (basically making a master for all of my spreadsheets). When I run it, I get the error pictured below, however, the file it cannot find is definitely in the folder. Can anyone help me solve this error? THANK YOU!

    Capture.PNG

    CODE:
    Sub LoopThroughDirectory()
    Dim MyFile As String
    Dim erow
    MyFile = Dir("C:\CE Sign In\")
    
    Do While Len(MyFile) > 0
    If MyFile = "zCE Class Sign In MASTER 2018.xlsm" Then
    Exit Sub
    End If
    
    Workbooks.Open (MyFile)
    Range("A2:O4").Copy
    ActiveWorkbook.Close
    
    erow = Sheet1.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Row
    ActiveSheet.Paste Destination:=Worksheets("Sheet1").Range(Cells(erow, 1), Cells(erow, 15))
    
    MyFile = Dir
    
    Loop
    
    End Sub
    Last edited by hhelgerson; 12-26-2017 at 05:02 PM. Reason: Issue Solved

  2. #2
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    48,322

    Re: VBA Code - Run-time Error 1004 can't find file

    Try:

    Option Explicit
    
    Sub LoopThroughDirectory()
    ' for testing
    Const sSourceFolder As String = "C:\Test\"
    ' for live work
    'Const sSourceFolder As String = "C:\CE Sign In\"
    
    Dim MyFile As String
    Dim erow
    MyFile = Dir(sSourceFolder)
    
    Do While Len(MyFile) > 0
    If MyFile = "zCE Class Sign In MASTER 2018.xlsm" Then
    Exit Sub
    End If
    
    Workbooks.Open sSourceFolder & MyFile
    Range("A2:O4").Copy
    ActiveWorkbook.Close
    
    erow = Sheet1.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Row
    Application.DisplayAlerts = False
    ActiveSheet.Paste _
        Destination:=Worksheets("Sheet1").Range(Cells(erow, 1), Cells(erow, 15))
    Application.DisplayAlerts = True
    
    MyFile = Dir
    
    Loop
    
    End Sub
    Trevor Shuttleworth - Retired Excel/VBA Consultant

    I dream of a better world where chickens can cross the road without having their motives questioned

    'Being unapologetic means never having to say you're sorry' John Cooper Clarke


  3. #3
    Registered User
    Join Date
    12-26-2017
    Location
    Portland, OR, USA
    MS-Off Ver
    2016
    Posts
    4

    Re: VBA Code - Run-time Error 1004 can't find file

    Thank you! This did solve my error from above however, now the macro is only pulling data from half of the workbooks. I have 4 saved in the folder right now and it is pulling data from 2. They are all the same file format so I'm not sure why its not pulling from them all. Are you familiar with this? Thanks!

  4. #4
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    48,322

    Re: VBA Code - Run-time Error 1004 can't find file

    What are the workbooks called?

  5. #5
    Registered User
    Join Date
    12-26-2017
    Location
    Portland, OR, USA
    MS-Off Ver
    2016
    Posts
    4

    Re: VBA Code - Run-time Error 1004 can't find file

    Issues.xlsm and Taxes.xlsm are the ones that work the macro. Green Energy.xlsm and Title.xlsm are the ones that don't.

  6. #6
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    48,322

    Re: VBA Code - Run-time Error 1004 can't find file

    Seems to work ok for me:

    Option Explicit
    
    ' Issues.xlsm and Taxes.xlsm are the ones that work the macro.
    ' Green Energy.xlsm and Title.xlsm are the ones that don't.
    
    Sub LoopThroughDirectory()
    ' for testing
    Const sSourceFolder As String = "C:\Test\"
    ' for live work
    'Const sSourceFolder As String = "C:\CE Sign In\"
    
    Dim MyFile As String
    Dim erow
    MyFile = Dir(sSourceFolder)
    
    Do While Len(MyFile) > 0
        Debug.Print "Read " & MyFile
        If MyFile = "zCE Class Sign In MASTER 2018.xlsm" Then
            Exit Sub
        End If
    
        Debug.Print "Process " & MyFile
    
        Workbooks.Open sSourceFolder & MyFile
        Range("A2:O4").Copy
        ActiveWorkbook.Close
        
        erow = Sheet1.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Row
        Application.DisplayAlerts = False
        ActiveSheet.Paste _
            Destination:=Worksheets("Sheet1").Range(Cells(erow, 1), Cells(erow, 15))
        Application.DisplayAlerts = True
    
        MyFile = Dir
    
    Loop
    
    End Sub
    
    'Debug Output
    'Read Green Energy.xlsm
    'Process Green Energy.xlsm
    'Read Issues.xlsm
    'Process Issues.xlsm
    'Read MonthEnd.xlsm
    'Process MonthEnd.xlsm
    'Read Taxes.xlsm
    'Process Taxes.xlsm
    'Read Title.xlsm
    'Process Title.xlsm
    'Read zCE Class Sign In MASTER 2018.xlsm

  7. #7
    Registered User
    Join Date
    12-26-2017
    Location
    Portland, OR, USA
    MS-Off Ver
    2016
    Posts
    4

    Re: VBA Code - Run-time Error 1004 can't find file

    Strange, I will keep trying. I appreciate all of your help!

  8. #8
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    48,322

    Re: VBA Code - Run-time Error 1004 can't find file

    I guess I/we would need to see samples of all live files with any sensitive data anonymised. It may be the files are being processed but maybe overwriting previous data.

+ 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. Error Code 1004 - Can't find File
    By Scacks in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 08-15-2017, 04:05 PM
  2. Run Time Error 1004 (VBA Code)
    By Cheezee in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 12-26-2015, 04:47 PM
  3. Replies: 4
    Last Post: 11-15-2013, 05:03 PM
  4. VBA Code...error = run time error 1004 autofilter method of range class failed
    By Dariusd7 in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 11-15-2013, 04:49 PM
  5. Run Time Error 1004 and I can't Find the Issue
    By tracymickeyd in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 06-07-2013, 02:37 PM
  6. Run Time Error 1004 on code
    By aftabn10 in forum Excel Programming / VBA / Macros
    Replies: 11
    Last Post: 02-24-2011, 09:46 AM
  7. Code Run-time error '1004'
    By superbob in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 02-11-2010, 09:18 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