Hey Everyone!
I've been trying to get an excel VBA macro to work by it keeps messing up.
The goal is for the macro to open every specific file in all the sub-folders of the main folders.
Example: If Main folder has 3 sub-folders called 1, 2, and 3, it'll go into each of those folders and pull the file, merge it into the Workbook running the macro.
However, at the moment, after it pulls the files, it reloops through the subfolders all over again.
I'm really new to this and can't figure out what's going wrong. Please help!
Script;
Sub GetSheets() Dim fso As Object
Dim folder As Object
Dim subfolders As Object
Dim MyFile As String
Dim wb As Workbook
Dim CurrFile As Object
With Application
.ScreenUpdating = False
.EnableEvents = False
.Calculation = xlCalculationManual
End With
Set fso = CreateObject("Scripting.FileSystemObject")
Set folder = fso.GetFolder("C:\Main Folder")
Set subfolders = folder.subfolders
MyFile = "c001.CSV"
For Each subfolders In subfolders
Set CurrFile = subfolders.Files
For Each CurrFile In CurrFile
Workbooks.Open Filename:=subfolders & "" & MyFile, ReadOnly:=True
For Each Sheet In ActiveWorkbook.Sheets
Sheet.Copy After:=ThisWorkbook.Sheets(1)
Next Sheet
Workbooks(MyFile).Close
Next
Next
Set fso = Nothing
Set folder = Nothing
Set subfolders = Nothing
With Application
.EnableEvents = True
.Calculation = xlCalculationAutomatic
.ScreenUpdating = True
End With
End Sub
I've been trying to get an excel VBA macro to work by it keeps messing up.
The goal is for the macro to open every specific file in all the sub-folders of the main folders.
Example: If Main folder has 3 sub-folders called 1, 2, and 3, it'll go into each of those folders and pull the file, merge it into the Workbook running the macro.
However, at the moment, after it pulls the files, it reloops through the subfolders all over again.
I'm really new to this and can't figure out what's going wrong. Please help!
Script;
Sub GetSheets() Dim fso As Object
Dim folder As Object
Dim subfolders As Object
Dim MyFile As String
Dim wb As Workbook
Dim CurrFile As Object
With Application
.ScreenUpdating = False
.EnableEvents = False
.Calculation = xlCalculationManual
End With
Set fso = CreateObject("Scripting.FileSystemObject")
Set folder = fso.GetFolder("C:\Main Folder")
Set subfolders = folder.subfolders
MyFile = "c001.CSV"
For Each subfolders In subfolders
Set CurrFile = subfolders.Files
For Each CurrFile In CurrFile
Workbooks.Open Filename:=subfolders & "" & MyFile, ReadOnly:=True
For Each Sheet In ActiveWorkbook.Sheets
Sheet.Copy After:=ThisWorkbook.Sheets(1)
Next Sheet
Workbooks(MyFile).Close
Next
Next
Set fso = Nothing
Set folder = Nothing
Set subfolders = Nothing
With Application
.EnableEvents = True
.Calculation = xlCalculationAutomatic
.ScreenUpdating = True
End With
End Sub