FINAL CODE:
Sub Consolidate()
' This macro imports (combines) all TSR workbooks into one sheet.
' This defines various objects
Dim fName As String, fPath As String, fPathDone As String, OldDir As String
Dim wbkOld As Workbook, wbkNew As Workbook, ws As Worksheet
' This speeds up the macro.
Application.ScreenUpdating = False
Application.EnableEvents = False
Application.DisplayAlerts = False
Application.Calculation = xlCalculationManual
' This defines that the current workbook is the detination for all the TSR workbooks to be copied too.
Set wbkNew = ThisWorkbook
wbkNew.Activate
Sheets("Invoice Data").Activate
ActiveSheet.Unprotect
' This sets the path of the folder where the TSR workbooks to be imported are stored.
OldDir = CurDir
With Application.FileDialog(msoFileDialogFolderPicker)
' The default path is the F drive.
.InitialFileName = ("F:\")
.Show
If .SelectedItems.Count > 0 Then
fPath = .SelectedItems(1)
Else
Exit Sub
End If
End With
ChDir fPath
fName = Dir("*.xl*")
' This imports the first sheet from each TSR workbook in the folder.
Do While Len(fName) > 0
Set wbkOld = Workbooks.Open(fName)
Sheets(1).Activate
' This copies only the given range of data from each TSR.
Range("A8:BP27").Copy
wbkNew.Sheets("Invoice Data").Cells(Rows.Count, "A").End(xlUp).Offset(1, 0).PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
' This closes the TSR workbooks and moves on to the next TSR workbook and the next empty row in the destination workbook.
wbkOld.Close False
fName = Dir
Loop
' This restores the original working path.
ChDir OldDir
' This clears data in where Column A (AFG#) is blank.
Sheets("Invoice Data").Activate
Range("A6:A10000").SpecialCells(xlCellTypeBlanks).Columns("A:BP").ClearContents
Range("A1").Select
' This reprotects the sheet.
ActiveSheet.Protect DrawingObjects:=True, Contents:=True, Scenarios:=True _
, AllowFormattingCells:=True, AllowFormattingColumns:=True, _
AllowFormattingRows:=True, AllowFiltering:=True, AllowUsingPivotTables:= _
True
' This resets the settings we changed to speed up the macro.
Application.ScreenUpdating = True
Application.EnableEvents = True
Application.DisplayAlerts = True
Application.Calculation = xlAutomatic
End Sub
Bookmarks