Importing data from from all .xls files inside folder

bdepizzol

New Member
Joined
Aug 1, 2011
Messages
5
Hi! I'm new here.

I have a workbook that handles data from several source files located in a folder (C:\documents\, for instance). I'm trying to create an Excel macro that creates one sheet for every file inside this existent workbook. It should create a new sheet (if it already doesn't exist), copy all the content inside this sheet and then close the data source.

The code I have so far is not working. It's the following:

Code:
Sub FindExcelFiles()
  Dim FileName As String
  Dim FolderPath As String
    FolderPath = "C:\Documents\"
    FileName = Dir(FolderPath & "\*.xls", vbNormal)
      Do While FileName <> ""
        FileName = Dir
        Workbooks.Open (FileName)

      Loop
End Sub</pre>

Can anyone help me?
 

Excel Facts

Get help while writing formula
Click the italics "fx" icon to the left of the formula bar to open the Functions Arguments dialog. Help is displayed for each argument.
Give this a try please:

Code:
Sub FindExcelFiles()
    Dim FileName As String
    Dim FolderPath As String
    Dim wb As Workbook
    FolderPath = "C:\Documents\"
    FileName = Dir(FolderPath & "\*.xls", vbNormal)
    Do While FileName <> ""
        Set wb = Workbooks.Open(FileName)
        wb.Sheets(1).Copy after:=ThisWorkbook.Worksheets(ThisWorkbook.Worksheets.Count)
        ThisWorkbook.Worksheets(ThisWorkbook.Worksheets.Count).Name = Left(Left(FileName, InStrRev(FileName, ".")), 32)
        FileName = Dir()
    Loop
End Sub
 
Upvote 0
I'm receiving this error message on line 8:

Run-time error '1004'
'<var>Filename</var>.xls' could not be found. Check the spelling of the file name, and verify that the file location is correct. If you are trying to open the file from your list of most recently used files on the File menu, make sure that the file has not been renamed, moved, or deleted.

Is there anything I can do?
 
Upvote 0
There was a small typo:

Code:
Sub FindExcelFiles()
    Dim FileName As String
    Dim FolderPath As String
    Dim wb As Workbook
    FolderPath = "C:\Documents\"
    FileName = Dir(FolderPath & "*.xls")
    Do While FileName <> ""
        Set wb = Workbooks.Open(FileName)
        wb.Sheets(1).Copy after:=ThisWorkbook.Worksheets(ThisWorkbook.Worksheets.Count)
        ThisWorkbook.Worksheets(ThisWorkbook.Worksheets.Count).Name = Left(Left(FileName, InStrRev(FileName, ".")), 32)
        FileName = Dir()
    Loop
End Sub
 
Upvote 0
That "\" after C:\Documents I had corrected on my own.
The error is still there.

My Excel is in portuguese. Does it make any difference?
 
Upvote 0
Oops, typo here:

Code:
Set wb = Workbooks.Open(FolderPath & FileName)

It's always a bit difficult since Dir() returns the file name it found, NOT the full path and filename. So we need to insert the folder path again when opening the returned file.
 
Upvote 0
Squeeze in this line of code:

Code:
wb.Close SaveChanges:=False
 
Upvote 0

Forum statistics

Threads
1,224,602
Messages
6,179,843
Members
452,948
Latest member
UsmanAli786

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