Option Explicit
Sub CopyAllWorksheeetsFromAllExcelFilesInThisFilesDirectory()
'Note last row of data in each worksheet is presumed to be the
'last row of Column A with data in it.
Dim strFileDirectory As String
Dim strFileName As String
Dim strWorksheetName As String
Dim iAnswer As Integer
Dim intFileCount As Integer
Dim lNextWriteRow As Long
Dim sError As String
Dim sReport As String
Dim lX As Long
Dim bFound As Boolean
Dim lLastDataRow As Long
Dim iVisibleWindows As Integer
Dim sPreface As String
Dim lNextSummaryWriteRow As Long
Dim lLinesCopied As Long
Dim secAutomation As MsoAutomationSecurity
secAutomation = Application.AutomationSecurity
'This workbook saved?
If ThisWorkbook.Path = "" Then
MsgBox "Save this file in the desired directory before continuing"
GoTo End_Sub
End If
For lX = 1 To Sheets.Count
If Worksheets(lX).Name = "Sheet1" Then
bFound = True
Exit For
End If
Next
If Not bFound Then Worksheets.Add(before:=Sheets(1)).Name = "Sheet1"
bFound = False
For lX = 1 To Sheets.Count
If Worksheets(lX).Name = "Summary" Then
bFound = True
Exit For
End If
Next
If Not bFound Then Worksheets.Add(after:=Sheets("Sheet1")).Name = "Summary"
'Close other workbooks - they may be ones we want to process
'and we don't want to overwrite them.
If Windows.Count > 1 Then
iAnswer = MsgBox("Close other workbooks and continue?" & vbCrLf & _
" OK to close all other workbooks and continue, or" & vbCrLf & _
" Cancel to stop this macro.", vbOKCancel + _
vbDefaultButton2 + vbExclamation, "Continue ?")
End If
If iAnswer = vbCancel Then
GoTo End_Sub
Else
iVisibleWindows = Windows.Count
For lX = Windows.Count To 1 Step -1
If Windows(lX).Caption <> ThisWorkbook.Name Then
If Windows(lX).Visible Then
'if workbook modified user will get
'chance to save or cancel for each
Windows(lX).Close
End If
iVisibleWindows = iVisibleWindows - 1
End If
Next
End If
strFileDirectory = ThisWorkbook.Path & "\"
'See if user chose Cancel for any close requests
If iVisibleWindows > 1 Then
MsgBox "Other Excel workbooks are still open. " & _
"Close other workbooks and try again", , "Process Cancelled."
GoTo End_Sub
End If
'More than this .xls? file in the directory?
strFileName = Dir(strFileDirectory & "*.xls?", 1)
Do While strFileName <> ""
intFileCount = intFileCount + 1
strFileName = Dir
Loop
If intFileCount = 1 Then
MsgBox "There are no other Excel files in the specified directory: " & vbCrLf & _
" " & strFileDirectory & vbCrLf & _
"There is nothing to process.", , "No Excel Files"
GoTo End_Sub
End If
iAnswer = MsgBox("All data on worksheets: 'Sheet1' and 'Summary' will be deleted. Continue?", vbOKCancel, "Clear Sheet1?")
If iAnswer = vbOK Then
With ThisWorkbook.Worksheets("Sheet1")
.UsedRange.Clear
For lX = .Shapes.Count To 1 Step -1
.Shapes(lX).Delete
Next
End With
ThisWorkbook.Worksheets("Summary").UsedRange.Clear
lNextWriteRow = 1
lNextSummaryWriteRow = 2
Worksheets("Summary").Range("A1").Resize(1, 4).Value = Array("Workbook Copied", "Worksheet Copied", "# Lines", "", "Total Lines Copied")
'Process other workbooks
strFileName = Dir(strFileDirectory & "*.xls?", 1)
Do While strFileName <> ""
If UCase(strFileName) <> UCase(ThisWorkbook.Name) And _
UCase(strFileName) <> "PERSONAL.XLS" And _
strFileName <> "PERSONAL.XLSM" Then
'Code won't run on newly opened files
'Application.AutomationSecurity = msoAutomationSecurityForceDisable
Workbooks.Open Filename:=strFileDirectory & strFileName, ReadOnly:=True, corruptload:=True, UpdateLinks:=False
'Application.AutomationSecurity = secAutomation 'restore original macro security
'Process file
For lX = 1 To Worksheets.Count
Worksheets(lX).Activate
strWorksheetName = Worksheets(lX).Name
Range("A1").Select 'in case workbook was saved with an object selected
lLastDataRow = Cells(Rows.Count, 1).End(xlUp).Row
If lLastDataRow + lNextWriteRow > Rows.Count Then
MsgBox "Next save would fill the worksheet. Quitting."
GoTo End_Sub
End If
Application.StatusBar = "Processing " & strFileName & ", " & strWorksheetName
If Range("A1") <> "" Or lLastDataRow > 1 Then
Worksheets(lX).Rows("1:" & lLastDataRow).Copy Destination:=ThisWorkbook.Sheets("Sheet1").Cells(lNextWriteRow, 1)
sReport = sReport & vbLf & lLastDataRow & vbTab & ActiveWorkbook.Name
ThisWorkbook.Worksheets("Summary").Cells(lNextSummaryWriteRow, 1) = strFileName
ThisWorkbook.Worksheets("Summary").Cells(lNextSummaryWriteRow, 2) = strWorksheetName
ThisWorkbook.Worksheets("Summary").Cells(lNextSummaryWriteRow, 3) = lLastDataRow
lNextSummaryWriteRow = lNextSummaryWriteRow + 1
lLinesCopied = lLinesCopied + lLastDataRow
End If
lNextWriteRow = ThisWorkbook.Worksheets("Sheet1").Cells(Rows.Count, 1).End(xlUp).Row + 1
Next
'Finished with opened workbook
ActiveWorkbook.Saved = True
Windows(strFileName).Close
End If
strFileName = Dir
Loop
ThisWorkbook.Worksheets("Summary").Cells(2, 4) = lLinesCopied
ThisWorkbook.Activate
Worksheets("Summary").Select
Columns("A:D").EntireColumn.AutoFit
Range("A1").Activate
sPreface = "For directory: " & ThisWorkbook.Path & vbLf & vbLf
If sReport <> "" Then sReport = sReport & vbLf & "------" & vbLf & _
lLinesCopied & vbTab & "Total Lines Copied"
If sError <> "" Then sReport = sReport & vbLf & vbLf & _
"The following workbooks did not contain a worksheet named 'Sheet1':" & vbLf
MsgBox sPreface & "Rows" & vbTab & "Copied from 'Sheet1'" & vbLf & "1 thru" & vbTab & " in each of these workbooks:" & vbLf & _
sReport & sError
Sheets("Sheet1").Copy 'Copy Sheet1 to new workbook
'Clear Sheet1 in this workbook
With ThisWorkbook.Worksheets("Sheet1")
.UsedRange.Clear
For lX = .Shapes.Count To 1 Step -1
.Shapes(lX).Delete
Next
End With
Else
MsgBox "Process Cancelled"
End If
End_Sub:
Application.AutomationSecurity = secAutomation 'restore original macro security
Application.StatusBar = False
End Sub