Sub BuildData()
Dim wsNew As Worksheet
Dim wsDest As Worksheet
Dim lastRow As Long
Dim lastCol As Long
Dim c As Range
'Where will data go?
Set wsDest = ThisWorkbook.Worksheets("Roster")
Application.ScreenUpdating = False
'Create a copy to work with
Worksheets("Raw").Copy after:=wsDest
Set wsNew = ActiveSheet
wsNew.Name = "HelperSheetDelete"
With wsNew
'Fill in our data
lastRow = .UsedRange.Rows.Count
.Range("C2:C" & lastRow).SpecialCells(xlCellTypeBlanks).FormulaR1C1 = "=R[-1]C"
'Extract agent number
.Range("D2:D" & lastRow).Formula = "=VALUE(TRIM(MID(SUBSTITUTE(C2,"" "",REPT("" "",999)),999,999)))"
'Fill Dates
For Each c In .Range("E:E").SpecialCells(xlCellTypeConstants)
c.Offset(3).FormulaR1C1 = "=R[-3]C"
Next c
'Convert to values
.Range("F2:F" & lastRow).Formula = "=DATEVALUE(MID(E10,FIND("" "",E10),999))"
'Convert numbers stored as text
.Range("M1").Value = 1
.Range("M1").Copy
.Range("L2:L" & lastRow).PasteSpecial xlPasteValues, xlPasteSpecialOperationMultiply
End With
With wsDest
lastRow = .Cells(.Rows.Count, "B").End(xlUp).Row
lastCol = .Cells(4, .Columns.Count).End(xlToLeft).Column
'Fill in our data
With .Range("E5", .Cells(lastRow, lastCol - 1))
.Formula = "=SUMIFS(HelperSheetDelete!$L:$L,HelperSheetDelete!$D:$D,$B5,HelperSheetDelete!$F:$F,E$4)"
.Copy
.PasteSpecial xlPasteValues
.NumberFormat = "h:mm"
End With
'Apply totals
.Range(.Cells(5, lastCol), .Cells(lastRow, lastCol)).FormulaR1C1 = "=SUM(RC5:RC[-1])"
End With
'Delete helper sheet
Application.DisplayAlerts = False
wsNew.Delete
Application.DisplayAlerts = True
'Clean-up
Application.Goto wsDest.Range("E5")
Application.ScreenUpdating = True
End Sub