Add Sheets

ryan8200

Active Member
Joined
Aug 21, 2011
Messages
357
I try to loop all csv files in folder and add them to current workbook but hit sheet not defined error. After I declare sheet, it hits run time error 1004. Method of open of object Workbooks failed. Anyone can help me on this error ?

VBA Code:
Sub AddCSV()
Dim wb As Workbook
Dim FSO As Object, Folder As Object, file As Object

Set FSO = CreateObject("Scripting.FileSystemObject")
Set Folder = FSO.GetFolder("C:\Users\My\Downloads\")
With Application
    .ScreenUpdating = False
    .EnableEvents = False
End With
For Each file In Folder.Files
    Set wb = Workbooks.Open(Filename:=file, UpdateLinks:=False, ReadOnly:=True, IgnoreReadOnlyRecommended:=True)
    For Each sheet In wb.Sheets
        sheet.Copy After:=ThisWorkbook.Sheets(1)
    Next
    wb.Close False
Next

End Sub
 

Excel Facts

Which came first: VisiCalc or Lotus 1-2-3?
Dan Bricklin and Bob Frankston debuted VisiCalc in 1979 as a Visible Calculator. Lotus 1-2-3 debuted in the early 1980's, from Mitch Kapor.
Hello!

Does your macro in Excel 97-2004 Workbook (.xls)? If yes, try to save it as Macro-enabled Workbook (.xlsm), close and open again.
A few minor changes:
VBA Code:
Sub AddCSV()
Dim wb As Workbook, sheet As Worksheet
Dim FSO As Object, Folder As Object, file As Object
    With Application
        .ScreenUpdating = False
        .EnableEvents = False
    End With
  
    Set FSO = CreateObject("Scripting.FileSystemObject")
    Set Folder = FSO.GetFolder("C:\Users\My\Downloads\")
  
    For Each file In Folder.Files
        If Right(file.Name, 4) = ".csv" Then
            Set wb = Workbooks.Open(Filename:=file, UpdateLinks:=False, ReadOnly:=True, IgnoreReadOnlyRecommended:=True)
            For Each sheet In wb.Sheets
                sheet.Copy After:=ThisWorkbook.Sheets(1)
            Next sheet
            wb.Close False
        End If
    Next file
End Sub
 
Upvote 0
Can I clarify, are you saying that you have tried @LazyBug's code and you are still getting an error ?
Can you show us an image of the error dialogue box.
An image of the line that is highlighted when you click on Debug in the error dialogue box
If you hover over the word "file" when it errors out what file name do you see ? If it is too hard to see it there, then in the immediate window (Ctrl+G if its not visible) type
? file it will show the file name in the immediate box. Is that the file with the chinese characters if not is there anything unusual about the file name you see ?
 
Upvote 0

Forum statistics

Threads
1,214,784
Messages
6,121,535
Members
449,037
Latest member
tmmotairi

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