数据透视表是要走的路。这将在第一个摘要中引发你的兴趣。您需要使用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