Use this code -
Option Explicit
Dim Master As Workbook
Dim sourceBook As Workbook
Dim sourceData As Worksheet
Dim CurrentFileName As String
Dim myPath As String
Dim frow As Long
Dim lrow As Long
Sub update_master()
'The folder containing the files to be recap'd
myPath = "D:\Test"
'Finds the name of the first file of type .xls in the current directory
CurrentFileName = Dir(myPath & "\*.xls")
'Create a workbook for the recap report
Set Master = ThisWorkbook
Do
Workbooks.Open (myPath & "\" & CurrentFileName)
Set sourceBook = Workbooks(CurrentFileName)
Set sourceData = sourceBook.Worksheets("Sales Log")
With sourceData
If Left(CurrentFileName, Len(CurrentFileName) - 5) = "Sales Person 1" Then
.Range("B3:E1002").Copy
Master.Worksheets("Sales Log").Range("B3:E1002").PasteSpecial , Paste:=xlValues
ElseIf Left(CurrentFileName, Len(CurrentFileName) - 5) = "Sales Person 2" Then
.Range("B1003:E2002").Copy
Master.Worksheets("Sales Log").Range("B1003:E2002").PasteSpecial , Paste:=xlValues
ElseIf Left(CurrentFileName, Len(CurrentFileName) - 5) = "Sales Person 3" Then
.Range("B2003:E3002").Copy
Master.Worksheets("Sales Log").Range("B2003:E3002").PasteSpecial , Paste:=xlValues
End If
End With
sourceBook.Close
'Calling DIR w/o argument finds the next .xlsx file within the current directory.
CurrentFileName = Dir()
Loop While CurrentFileName <> ""
ActiveWorkbook.Worksheets("Sales Log").Sort.SortFields.Clear
ActiveWorkbook.Worksheets("Sales Log").Sort.SortFields.Add Key:=Range( _
"B:B"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
xlSortNormal
With ActiveWorkbook.Worksheets("Sales Log").Sort
.SetRange Range("B:F")
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
End Sub
You can retain the sort code or remove it if not required.
Bookmarks