Private Sub LoopThroughXLS_ConsolidateOneSheet()
'opens all workbooks in a folder and consolidate them in one sheet on the active workbook
application.ScreenUpdating = False 'disable screen updating to avoid screen flashing
Dim wbk1 As Workbook, wbk2 As Workbook, i As Long, strWbk As String, sht1 As Worksheet
Dim blFirst As Boolean
Dim strWbkNames(1 To 500) As String, FileName As String, intFileCount As Long, intWbkLoop As Long
'***************UPDATE PARAMETERS HERE*************************
Const strpath As String = "C:\Temp\test" 'Change to the folders where you drop your files
Const blHeader As Boolean = True 'true if your data has headers, false otherwise
'**************************************************************
Set wbk1 = ActiveWorkbook
Set sht1 = ActiveSheet
blFirst = True
FileName = Dir(strpath & "\*.xls")
Do While FileName <> ""
intFileCount = intFileCount + 1
strWbkNames(intFileCount) = FileName
FileName = Dir
Loop
application.DisplayAlerts = False
For intWbkLoop = 1 To intFileCount
If application.WorksheetFunction.CountA(sht1.Columns(1)) = 0 Then
sht1.range("A1").Select
Else
sht1.range("A" & sht1.Rows.Count).End(xlUp).Offset(1, 0).Select
End If
Set wbk2 = Workbooks.Open(strpath & "\" & strWbkNames(intWbkLoop))
With wbk2.Sheets(1)
If blFirst = True Then
blFirst = False
.UsedRange.Copy
Else
If blHeader = True Then
.UsedRange.Offset(1).Copy
Else
.UsedRange.Copy
End If
End If
End With
sht1.Paste
wbk2.Close (False)
Next
application.DisplayAlerts = True
application.ScreenUpdating = True
End Sub
|