Awesome it compiled! Many thanks.
The mechanisim is working perfectly.
However one quick problem:
The main goal is to open the csv file and paste contents into the target (main) WB on separate sheets. I did this by tracking the sheet index. I made a variable called sheet_index which I added +1 to everytime through the loop, this told the macro to paste the data onto the next sheet, or create a new sheet and paste depending on how many there were in the work book. NOW I can't use that method it seems. Below I have posted a shortened version of the main code. Currently, it just copies everything onto the same sheet...
Sub WLDO_Driver()
Dim targetBook As Workbook
Set targetBook = ActiveWorkbook
Dim i As Integer
'creates summary sheet in target WB
targetBook.Activate
Sheets.Add.Name = "SUMMARY"
Call LoopThroughDirectory(targetBook, "C:\Users\E1TCH02\Desktop\Input", "csv", True)
'creates chart
targetBook.Activate
Sheets("SUMMARY").Select
Range("D2").Select
ActiveSheet.Shapes.AddChart.Select
With ActiveChart
.ChartType = xlLine
.SeriesCollection.NewSeries
.SeriesCollection(1).Values = Sheets("SUMMARY").Range("a1", ActiveSheet.Range("a1").End(xlDown))
.Axes(xlCategory).Select
End With
Selection.Delete
targetBook.Save
End Sub
Sub LoopThroughDirectory(ByRef SomeWorkbook As Workbook, myPath As String, fileExtension As String, incSub As Boolean)
Dim fso As FileSystemObject
Dim fld As Folder, subfld As Folder
Dim fil As File
Dim N As Long
Dim sheet_index As Integer
sheet_index = 1
'initialize'
Set fso = New FileSystemObject
Set fld = fso.GetFolder(myPath)
'place each file name in the array'
For Each fil In fld.Files
If UCase(fil.Name) Like "*." & UCase(fileExtension) Then
Application.DisplayAlerts = False
Set WB = Workbooks.Open(fil.Path)
'copy results from the just opened wb
WB.Worksheets(1).Select
Range("a1:s2200").Select
Selection.Copy
WB.Close
'go to main wb and paste data
SomeWorkbook.Activate
If sheet_index <= 4 Then
Sheets(sheet_index).Select
ActiveSheet.Paste
Else
Sheets.Add
ActiveSheet.Paste
End If
'I adjust the newly pasted data
' 'take wanted information from each sheet and paste onto summary sheet
Range("G11:H12").Select
Selection.Copy
Sheets("SUMMARY").Select
If Range("a1").Value = "" Or Range("a2").Value = 0 Then
Range("a1").Select
Else
Range("a1").End(xlDown).Select
ActiveCell.Offset(rowOffset:=1, columnOffset:=0).Select
End If
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
Columns("A:A").EntireColumn.AutoFit
'
'save main workbook
SomeWorkbook.Activate
SomeWorkbook.Save
End If
sheet_index = sheet_index + 1
Next fil
'pass in the subfolders if incSub is passed in as "True"
If incSub Then
For Each subfld In fld.SubFolders
Call LoopThroughDirectory(SomeWorkbook, myPath & "\" & subfld.Name, fileExtension, True)
Next subfld
End If
End Sub
Bookmarks