sans,
For something like this where you need to make changes at the level of each subfolder instead of at the level of each file, the GetAllFiles sub needed some modification.
Adjusted tgr code:
Sub tgr()
Dim ws As Worksheet
Dim lSec As MsoAutomationSecurity
Dim arrFiles As Variant
Dim varFilePath As Variant
With Application
lSec = .AutomationSecurity
.AutomationSecurity = msoAutomationSecurityForceDisable
.ScreenUpdating = False
.DisplayAlerts = False
End With
GetAllFiles "C:\Test2", "xls*", True
With Application
.AutomationSecurity = lSec
.ScreenUpdating = True
.DisplayAlerts = True
End With
End Sub
Adjusted GetAllFiles code:
Public Sub GetAllFiles(ByVal strFolderPath As String, Optional ByVal strExt As String = "*", Optional ByVal bCheckSubfolders As Boolean = False)
Dim wbDest As Workbook
Dim FSO As Object
Dim oFile As Object
Dim oFolder As Object
Dim strName As String
Dim sSep As String
Dim strNewFilePath As String
Dim i As Long
Dim bNoData As Boolean
sSep = Application.PathSeparator
Set FSO = CreateObject("Scripting.FileSystemObject")
Set wbDest = Workbooks.Add
strNewFilePath = strFolderPath
Select Case (Right(strNewFilePath, Len(sSep)) = sSep)
Case True: strName = Mid(strFolderPath, InStrRev(Left(strFolderPath, Len(strFolderPath) - Len(sSep)), sSep) + Len(sSep))
strNewFilePath = strFolderPath & strName & ".xlsm"
Case Else: strName = Mid(strFolderPath, InStrRev(strFolderPath, sSep) + Len(sSep))
strNewFilePath = strNewFilePath & sSep & strName & ".xlsm"
End Select
For Each oFile In FSO.GetFolder(strFolderPath).Files
If LCase(FSO.GetExtensionName(oFile.Path)) Like LCase(strExt) Then
With Workbooks.Open(oFile.Path)
.Sheets(1).Copy After:=wbDest.Sheets(wbDest.Sheets.Count)
.Close False
End With
Kill oFile.Path
End If
Next oFile
bNoData = False
For i = wbDest.Sheets.Count To 1 Step -1
If wbDest.Sheets(i).UsedRange.Address = "$A$1" And Len(wbDest.Sheets(i).Range("A1").Value) = 0 Then
Select Case (wbDest.Sheets.Count > 1)
Case True: wbDest.Sheets(i).Delete
Case Else: bNoData = True
End Select
End If
Next i
wbDest.SaveAs strNewFilePath, xlOpenXMLWorkbookMacroEnabled
wbDest.Close False
If bNoData = True Then Kill strNewFilePath
If bCheckSubfolders = True Then
For Each oFolder In FSO.GetFolder(strFolderPath).SubFolders
GetAllFiles oFolder.Path, strExt, True
Next oFolder
End If
Set wbDest = Nothing
Set FSO = Nothing
End Sub
Bookmarks