Good Morning
I have this macro, but I need it to also read .xls files from the subfolders too.... Can someone help me please?
Option Explicit
Sub BatchNo_Strzyzynski()
Dim myPath As String, FilesInPath As String
Dim MyFiles() As String
Dim SourceRcount As Long, FNum As Long
Dim mybook As Workbook, BaseWks As Worksheet
Dim sourceRange As Range, destRange As Range
Dim rnum As Long, CalcMode As Long
Dim NumOfRecordsInSourceFile As Long
Const FirstDataRowInSourceFile As Long = 10 '#### change this if necessary
'Fill in the path\folder where the files are
' MyPath = "H:\NewItemAdd\NPI Spreadsheets\0 - Panama\2 - Categorized\6 - June 2013\Strzyznski"
myPath = "H:\NewItemAdd\NPI Spreadsheets\0 - Panama\2 - Categorized\6 - June 2013\Strzyzynski"
'Add a slash at the end if the user forget it
If Right(myPath, 1) <> "\" Then
myPath = myPath & "\"
End If
'If there are no Excel files in the folder exit the sub
FilesInPath = Dir(myPath & "*.xl*")
If FilesInPath = "" Then
MsgBox "No spreadsheets found"
Exit Sub
End If
'Fill the array(myFiles)with the list of Excel files in the folder
FNum = 0
Do While FilesInPath <> ""
FNum = FNum + 1
ReDim Preserve MyFiles(1 To FNum)
MyFiles(FNum) = FilesInPath
FilesInPath = Dir()
Loop
'Change ScreenUpdating, Calculation and EnableEvents
With Application
CalcMode = .Calculation
.Calculation = xlCalculationManual
.ScreenUpdating = False
.EnableEvents = False
End With
'Add a new workbook with one sheet
Set BaseWks = Workbooks.Add(xlWBATWorksheet).Worksheets(1)
With ActiveSheet
.Cells(1, 1).Value = "Spreadsheet Name"
.Cells(1, 2).Value = "SS Info"
.Cells(1, 3).Value = "SKUs Count"
End With
rnum = 2 '### changed to include Headers
'Loop through all files in the array(myFiles)
If FNum > 0 Then
For FNum = LBound(MyFiles) To UBound(MyFiles)
Set mybook = Nothing
On Error Resume Next
If myPath & MyFiles(FNum) <> ThisWorkbook.FullName Then
Set mybook = Workbooks.Open(myPath & MyFiles(FNum))
On Error GoTo 0
If Not mybook Is Nothing Then
'################################################################## most changes made in this section
On Error Resume Next
With mybook.Worksheets(1)
Set sourceRange = .Range("F1:F8") '######### rephrased the range from "B4:B4"
' count the number of records in the source file from row 11 downwards for all _
'filled rows (until the first blank is encountered)
'It shouldn't be a problem in this case but please "note that a not-yet calculated cell returns Empty..." _
If IsEmpty(.Range("a" & FirstDataRowInSourceFile)) Then
NumOfRecordsInSourceFile = 0
Else
NumOfRecordsInSourceFile = _
.Range(.Range("a" & FirstDataRowInSourceFile), .Range("a" & FirstDataRowInSourceFile).End(xlDown)).Rows.Count - 1
End If
End With
If Err.Number > 0 Then
Err.Clear
Set sourceRange = Nothing
Else
'if SourceRange use all columns then skip this file
If sourceRange.Columns.Count >= BaseWks.Columns.Count Then
Set sourceRange = Nothing
End If
End If
On Error GoTo 0
If Not sourceRange Is Nothing Then
SourceRcount = sourceRange.Rows.Count
If rnum + SourceRcount >= BaseWks.Rows.Count Then
MsgBox "Sorry there are not enough rows in the sheet"
BaseWks.Columns.AutoFit
mybook.Close savechanges:=False
GoTo ExitTheSub
Else
'Copy the file name in column A
With sourceRange
BaseWks.Cells(rnum, "A"). _
Resize(.Rows.Count).Value = MyFiles(FNum)
End With
'Set the destrange
Set destRange = BaseWks.Range("B" & rnum)
'we copy the values from the sourceRange to the destrange
With sourceRange
Set destRange = destRange. _
Resize(.Rows.Count, .Columns.Count)
End With
With destRange
.Value = sourceRange.Value
.Offset(0, 1).Value = NumOfRecordsInSourceFile '###### added by Rob
End With
'#############################################################
rnum = rnum + SourceRcount
End If
End If
mybook.Close savechanges:=False
End If
Else
On Error GoTo 0
End If
Next FNum
BaseWks.Columns.AutoFit
End If
ExitTheSub:
'Added
Set mybook = Nothing
Set sourceRange = Nothing
Set destRange = Nothing
'Restore ScreenUpdating, Calculation and EnableEvents
With Application
.ScreenUpdating = True
.EnableEvents = True
.Calculation = CalcMode
End With
Beep
MsgBox "Done", , "Macro Complete"
End Sub
Bookmarks