[FONT=Fixedsys]Option Explicit[/FONT]
[FONT=Fixedsys][/FONT]
[FONT=Fixedsys]Public Sub CreatePivotTable()[/FONT]
[FONT=Fixedsys][/FONT]
[FONT=Fixedsys] Const SourceData As String = "[COLOR=red]Sheet1[/COLOR]"
Const OutputData As String = "[COLOR=red]Sheet2[/COLOR]"
Const PivotTablename As String = "MyPivotTable"
Dim sws As Worksheet
Dim tws As Worksheet
Dim iLastRow As Long
Dim iRow As Long
Dim iNextRow As Long
Dim iDate As Long
Dim objPivotItem As PivotItem
Set sws = ThisWorkbook.Sheets(SourceData)
On Error Resume Next
Set tws = ThisWorkbook.Sheets(OutputData)
If Not tws Is Nothing Then
Application.DisplayAlerts = False
ThisWorkbook.Sheets(OutputData).Delete
Application.DisplayAlerts = False
End If
On Error GoTo 0
ThisWorkbook.Worksheets.Add After:=sws
ActiveSheet.Name = OutputData
Set tws = ThisWorkbook.Sheets(OutputData)
tws.Range("A1") = "trip date"
tws.Range("B1") = "car"
[COLOR=green] ' pass 1: expand data - each trip is expanded to a series of single day trips
[/COLOR] iLastRow = sws.Cells(sws.Rows.Count, "A").End(xlUp).Row
iNextRow = 1
For iRow = 2 To iLastRow
For iDate = sws.Cells(iRow, "A").Value To sws.Cells(iRow, "B").Value
iNextRow = iNextRow + 1
tws.Cells(iNextRow, "A") = iDate
tws.Cells(iNextRow, "A").NumberFormat = "d/m/yyyy"
tws.Cells(iNextRow, "B") = sws.Cells(iRow, "C").Value
Next iDate
Next iRow
[COLOR=green] ' pass 2: sort data - so we can dedupe it next
[/COLOR] iLastRow = tws.Cells(tws.Rows.Count, "A").End(xlUp).Row
With tws.Sort
.SortFields.Clear
.SortFields.Add Key:=Range("B2:B" & iLastRow), SortOn:=xlSortOnValues, _
Order:=xlAscending, DataOption:=xlSortNormal
.SortFields.Add Key:=Range("A2:A" & iLastRow), SortOn:=xlSortOnValues, _
Order:=xlAscending, DataOption:=xlSortNormal
.SetRange Range("A2:B" & iLastRow)
.Header = xlGuess
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With[/FONT]
[FONT=Fixedsys][/FONT]
[FONT=Fixedsys][COLOR=green] ' pass 3: dedupe data
[/COLOR] For iRow = iLastRow - 1 To 2 Step -1
If tws.Cells(iRow, "B").Value = tws.Cells(iRow + 1, "B").Value Then
If tws.Cells(iRow, "A").Value = tws.Cells(iRow + 1, "A").Value Then
tws.Rows(iRow + 1).EntireRow.Delete
End If
End If
Next iRow
[COLOR=green] ' pass 4: make pivot table
[/COLOR][COLOR=#008000][COLOR=black] iLastRow = tws.Cells(tws.Rows.Count, "A").End(xlUp).Row
ThisWorkbook.PivotCaches.Create( _
SourceType:=xlDatabase, SourceData:="Sheet2!R1C1:R" & iLastRow & "C2", _
Version:=xlPivotTableVersion12).CreatePivotTable _
TableDestination:=OutputData & "!R1C4", TableName:=PivotTablename, _
DefaultVersion:=xlPivotTableVersion12
tws.PivotTables(PivotTablename).AddDataField _
tws.PivotTables(PivotTablename).PivotFields(tws.Range[/COLOR][COLOR=black]("B1").Value), _
"Trips/car", xlCount
With tws.PivotTables(PivotTablename).PivotFields(tws.Range("B1").Value)
.Orientation = xlRowField
.Position = 1
End With
tws.Range("D1") = "Cars"
With tws.PivotTables(PivotTablename).PivotFields(tws.Range("A1").Value)
.Orientation = xlRowField
.Position = 2
[/COLOR] [/COLOR][COLOR=black] End With
[COLOR=green] ' hide details per car - comment this out to show details per car
[/COLOR] With tws.PivotTables(PivotTablename).PivotFields(tws.Range("B1").Value)
For Each objPivotItem In .PivotItems
objPivotItem.ShowDetail = False
Next objPivotItem
End With
End Sub[/COLOR][/FONT]