數據透視表是要走的路。這將在第一個摘要中引發你的興趣。您需要使用Excel 2013作爲第二個,才能夠爲15分鐘的塊選擇DISTINCT計數。
Dim wb As Workbook
Dim sourceWorksheet As Worksheet
Dim summaryWorksheet As Worksheet
Dim pvt1 As PivotTable
Dim firstCell As Range
Dim lastRow As Long
Set wb = ActiveWorkbook
Set sourceWorksheet = wb.ActiveSheet
'Add two extra columns to calculate 15 minute blocks
'===================================================
'***** SET THIS TO THE TOP-LEFT CELL OF THE DATA THAT CONTAINS "Hours" LABEL
Set firstCell = sourceWorksheet.Range("A4")
'Determine the last row
lastRow = firstCell.CurrentRegion.Rows.Count + 3
'Put column captions on new columns
Cells(firstCell.Row, firstCell.Column + 4).Value = "Start"
Cells(firstCell.Row, firstCell.Column + 5).Value = "End"
'Set the formula for "Start" of 15 minute block
Range(Cells(firstCell.Row + 1, firstCell.Column + 4), Cells(lastRow, firstCell.Column + 4)).Formula = "=FLOOR(" & Cells(firstCell.Row + 1, firstCell.Column).Address(False, False) & ",TIME(0,15,0))"
'Set the formula for "End" of 15 minute block
Range(Cells(firstCell.Row + 1, firstCell.Column + 5), Cells(lastRow, firstCell.Column + 5)).Formula = "=" & Cells(firstCell.Row + 1, firstCell.Column + 4).Address(False, False) & "+""00:15"""
'Format the columns as times
Range(Cells(firstCell.Row, firstCell.Column + 4), Cells(lastRow, firstCell.Column + 5)).NumberFormat = "hh:mm"
'Create a pivot table for summary by 15 minute block
'===================================================
Set summaryWorksheet = Sheets.Add
summaryWorksheet.Name = "Summary"
Set pvt1 = wb.PivotCaches.Create(SourceType:=xlDatabase, _
SourceData:=sourceWorksheet.Name & "!" & firstCell.CurrentRegion.Address(ReferenceStyle:=xlR1C1), _
Version:=xlPivotTableVersion15).CreatePivotTable(TableDestination:=summaryWorksheet.Name & "!R3C1", _
TableName:="PivotTable1", _
DefaultVersion:=xlPivotTableVersion15)
With pvt1.PivotFields("Start")
.Orientation = xlRowField
.Position = 1
End With
With pvt1.PivotFields("End")
.Orientation = xlRowField
.Position = 2
End With
With pvt1.PivotFields("User")
.Orientation = xlRowField
.Position = 3
End With
pvt1.AddDataField pvt1.PivotFields("Cases"), "Sum of Cases", xlSum
'Change the style to be tabular
pvt1.RowAxisLayout xlTabularRow
'Remove unwanted subtotals
pvt1.PivotSelect "End[All;Total]", xlDataAndLabel, True
pvt1.PivotFields("End").Subtotals = Array(False, False, False, False, False, False, False, False, False, False, False, False)
pvt1.PivotSelect "Start[All;Total]", xlDataAndLabel, True
pvt1.PivotFields("Start").Subtotals = Array(False, False, False, False, False, False, False, False, False, False, False, False)
pvt1.PivotSelect "Start[All]", xlLabelOnly, True
Range("A3").Select
'Be a good memory citizen
Set wb = Nothing
Set sourceWorksheet = Nothing
Set summaryWorksheet = Nothing
Set pvt1 = Nothing
Set pvt2 = Nothing
Set firstCell = Nothing
你爲什麼需要VBA?如果我正確理解您的需求,簡單的Excel Sumifs和Countifs函數可以爲您做到這一點。 – vacip
或者像python或php這樣的腳本語言會很快做到你想要的。 –
如果我將在休息日,其他人不得不這樣做。我不知道關於公式的理解和擅長這個其他人,所以我寧願讓宏在粘貼相關日期後自動執行它。其次,我無法想象公式如何看起來像。 不幸的是只有腳本語言我可以使用VBA。我不能使用其他任何東西。 – LukeJ