vba to import worksheets from folder was working yesterday but not today

reece1984

New Member
Joined
May 15, 2012
Messages
18
Hi All,

Any help would be gratefully received on this.

I have a macro which loops through various worksheets that are contained in a directory and copies them into a master workbook.

The problem is that yesterday my importloop macro was working perfectly but now I am receiving Run-time error '1004':

'name_of_the_first_file.xlsx' could not be found.....

The vba is managing to read the filename perfectly so it must be finding it but something is clearly not working. Does anybody know why this is?


Here is the code for my macro.

Code:
 Sub ImportLoop()
    Dim MyObj As Object, MySource As Object, file As Variant
    If MsgBox("Are you sure that you want to import sheets?", vbYesNo, "Confirmation... ") = vbNo Then Exit Sub
    ChDir _
        "X:\Import files directory"
   
   SourceDir = "X:\Import files directory"
   
   file = Dir(SourceDir)
   
   While (file <> "")
    
    Workbooks.Open Filename:=file, UpdateLinks:=0
    
         
    Sheets.Copy Before:=Workbooks("Master Workbook.xlsm").Sheets(4)
    Windows(file).Activate
    ActiveWindow.Close False
     file = Dir

  Wend
End Sub

Thank you
 

Excel Facts

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.
From Help:
***********
The ChDir statement changes the default directory but not the default drive. For example, if the default drive is C, the following statement changes the default directory on drive D, but C remains the default drive:

<code>ChDir "D:\TMP"
************

So you may have to use the CurDir function too.


</code>
 
Upvote 0

Forum statistics

Threads
1,216,098
Messages
6,128,812
Members
449,468
Latest member
AGreen17

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top