Sub Downtimer()
Dim rg As Range, rgdata As Range
Application.ScreenUpdating = False
With ActiveWorkbook.Worksheets(1)
.Range("E1") = "Downtime"
Set rg = .Range("A1").CurrentRegion
Set rgdata = rg.Cells(2, 1).Resize(rg.Rows.Count - 1, 5)
'Use either the next statement (Excel 2010 or later) or the two that follow it (Excel 2007)
'rgdata.Columns(5).FormulaR1C1 = _
"=RC[-2]-IFERROR(AGGREGATE(15,6,R1C[-1]:R[-1]C[-1]/((R1C[-4]:R[-1]C[-4]=RC[-4])*(R1C[-3]:R[-1]C[-3]=RC[-3])),1),TIME(9,0,0))"
rgdata.Cells(1, 5).FormulaArray = _
"=RC[-2]-MAX(IFERROR(MIN(IF((R1C[-4]:R[-1]C[-4]=RC[-4])*(R1C[-3]:R[-1]C[-3]=RC[-3])=1,R1C[-1]:R[-1]C[-1],"""")),0),TIME(9,0,0))"
rgdata.Cells(1, 5).AutoFill Destination:=rgdata.Columns(5)
rgdata.Columns(5).NumberFormat = "h:mm:ss;@"
.Sort.SortFields.Clear
.Sort.SortFields.Add Key:=rgdata.Columns(1), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
.Sort.SortFields.Add Key:=rgdata.Columns(2), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
.Sort.SortFields.Add Key:=rgdata.Columns(3), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
With .Sort
.SetRange rg
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
End With
PTmaker rg
End Sub
Sub PTmaker(rg As Range)
Dim shName As String
Dim wb As Workbook
With rg.Worksheet
Set wb = .Parent
shName = "'" & .Name & "'!"
wb.PivotCaches.Create(SourceType:=xlDatabase, SourceData:=shName & rg.Address(ReferenceStyle:=xlR1C1)).CreatePivotTable _
TableDestination:=shName & "R1C8", TableName:="PivotTable1"
With .PivotTables("PivotTable1")
With .PivotFields("Extn")
.Orientation = xlColumnField
.Position = 1
End With
With .PivotFields("Date")
.Orientation = xlRowField
.Position = 1
End With
.AddDataField .PivotFields("Downtime"), "Sum of Downtime", xlSum
With .PivotFields("Downtime")
.Orientation = xlRowField
.Position = 2
End With
.DataBodyRange.NumberFormat = "h:mm:ss;@"
End With
End With
End Sub
Microsoft Excel topics include formulas, formatting, VBA macros and user-defined functions, and everything else related to the spreadsheet user interface, including error messages.
TRUSTED BY
If you sort the data in ascending order by date, extension & time, you can calculate the amount of logged out time preceding each session using a formula like:
=C2-IFERROR(AGGREGATE(15,6
The preceding formula requires Excel 2010 or later. Please advise if you are using an earlier version of Excel, as alternative formulas are possible.