Run-time error '1004': File could not be found

goss

Active Member
Joined
Feb 2, 2004
Messages
372
Hi all,

Using Excel 2010
I also have Excel 2013 Beta installed

My code returns the error
Run-time error '1004': File could not be found. Check the spelling of the file name and verify that the file location is correct...

I am using FileSystemObject and I have reference set to Microsoft Scripting Runtime.
I made sure the file reported does exist in the location specified.
The files in the folder are in .xls format

My debug line returns the first filename as expected

Debug points to this line
Code:
         Workbooks.Open (oFile.Name)

Any idea what might be wrong?

thx
w

Code:
    'Declare variables
     Dim wb As Workbook
     Dim wbCurr As Workbook
     Dim ws As Worksheet
     Dim FSO As FileSystemObject
     Dim oFolder As Folder
     Dim oFile As File
     Dim rng As Range
     Dim rngData As Range
     Dim rngDest As Range
     Dim rngNew As Range
     Dim rngCrit As Range
     Dim arrCrit
     Dim varrFilter As Variant
     Dim strPathIn As String
     Dim strPathOutput As String
     Dim strFileOutput As String
     Dim strOpFile As String
     Dim lngRows As Long
     Dim lngRowsCriteria As Long
     Dim i As Long
     Dim FileFormatValue As Long
     
    'Initialize variables
     Set wb = ThisWorkbook
     strPathIn = "C:\Countries\" '<-Change as needed
     
    'Instantiate FileSystemObject
     Set FSO = New FileSystemObject
     
    'Loop through the files in the folder
     For Each oFile In FSO.GetFolder(strPathIn).Files
     Debug.Print oFile.Name
         Workbooks.Open (oFile.Name)
         strOpFile = ActiveWorkbook.Name
         Windows(strOpFile).Activate
         Set wbCurr = Application.ActiveWorkbook
         Set ws = wbCurr.Worksheets("Sheet1")
 

Excel Facts

Easy bullets in Excel
If you have a numeric keypad, press Alt+7 on numeric keypad to type a bullet in Excel.
You aren't including the file path.
 
Upvote 0
If the ofile is not in the current directory (?CURDIR in the immediate pane) then openfile wants to see the path too.
Try using .path instead of .Name (.path includes the filename)
 
Upvote 0
If the ofile is not in the current directory (?CURDIR in the immediate pane) then openfile wants to see the path too.
Try using .path instead of .Name (.path includes the filename)

Thanks p45cal,

Using .path instead of .name did the job.

Thanks
goss
 
Upvote 0

Forum statistics

Threads
1,215,219
Messages
6,123,692
Members
449,117
Latest member
Aaagu

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