All code changes given in bold.
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
Dim FName as string
Private Sub update_master()
'
'Open an Alert that I can display information with OK or Cancel.
Msgbox "Your message here", vbokcancel
'Prepare This Workbook: On macro start, unprotect sheet, sort Range ("B3:B100") ascending
worksheets("SALES LOG").UnProtect Password:="456"
Columns("B:B").Select
ActiveWorkbook.Worksheets("SALES LOG").Sort.SortFields.Clear
ActiveWorkbook.Worksheets("SALES LOG").Sort.SortFields.Add Key:=Range("B3:B100") _
, SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
With ActiveWorkbook.Worksheets("SALES LOG").Sort
.SetRange Range("B:B")
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
'The folder containing the files to be recap'd
myPath = "C:\Test"
'Finds the name of the first file of type .xlsm in the current directory
CurrentFileName = Dir(myPath & "\*.xlsm")
'Create a workbook for the recap report
Set Master = ThisWorkbook
'With each file: On open, unprotect sheet, sort Range ("B3:B100") ascending before copying range
Do
Workbooks.Open (myPath & "\" & CurrentFileName)
Set sourceBook = Workbooks(CurrentFileName)
Set sourceData = sourceBook.Worksheets("SALES LOG")
With sourceData
.Unprotect Password:="456"
.Columns("B:B").Select
.Sort.SortFields.Clear
.Sort.SortFields.Add Key:=Range("B3:B100") _
, SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
With sourcedata.Sort
.SetRange Range("B:B")
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
If Left(CurrentFileName, Len(CurrentFileName) - 5) = "Salesperson1" Then
Call PreparetoCopy
.Range("RANGE1").Copy
Master.Worksheets("SALES LOG").Range("RANGE1").PasteSpecial , Paste:=xlValues
ElseIf Left(CurrentFileName, Len(CurrentFileName) - 5) = "Salesperson2" Then
Call PreparetoCopy
.Range("RANGE2").Copy
Master.Worksheets("SALES LOG").Range("RANGE2").PasteSpecial , Paste:=xlValues
ElseIf Left(CurrentFileName, Len(CurrentFileName) - 5) = "Salesperson3" Then
Call PreparetoCopy
.Range("RANGE3").Copy
Master.Worksheets("SALES LOG").Range("RANGE3").PasteSpecial , Paste:=xlValues
End If
End With
sourceBook.Close
'Calling DIR w/o argument finds the next .xlsm file within the current directory.
CurrentFileName = Dir()
Loop While CurrentFileName <> ""
'With current workbook Select Range ("RANGE_ALL") and sort ascending by Range("H3:H100").
Activeworkbook.worksheets("SALES LOG").range("RANGE_ALL").Select
ActiveWorkbook.Worksheets("SALES LOG").Sort.SortFields.Clear
ActiveWorkbook.Worksheets("SALES LOG").Sort.SortFields.Add Key:=Range("HB3:H100") _
, SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
With ActiveWorkbook.Worksheets("SALES LOG").Sort
.SetRange Range("H:H")
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
'Save This Workbook: File Name, Current Date and Time. Ignore Alerts. Attributes=Read Only
Fname=date & " " & time
ThisWorkbook.SaveAs Filename:=Fname, Fileformat:=xlopenxlmworkbook, createbackup:=false
'Save current workbook File Name, Current Date and Time. Ignore Alerts not working.
Application.DisplayAlerts = False
ActiveWorkbook.SaveAs Filename:="C:\Test\SALES LOG1.xlsm", FileFormat:= _
xlOpenXMLWorkbookMacroEnabled, CreateBackup:=False
ActiveWorkbook.SaveAs Filename:="C:\Test\SALES LOG2.xlsm", FileFormat:= _
xlOpenXMLWorkbookMacroEnabled, CreateBackup:=False
ActiveWorkbook.SaveAs Filename:="C:\Test\SALES LOG3.xlsm", FileFormat:= _
xlOpenXMLWorkbookMacroEnabled, CreateBackup:=False
ThisWorkbook.Close
'Close Excel Application.
End Sub
Bookmarks