This is going to be a basic question. I've looked online and have formulated what I believe to be correct. The worksheet will generate but it doesn't display any data. Is it the headers? What is wrong? I just want a pivot table that compares Columns F:I.
My source data is formulated as follows:
Sub PIVOT()
Dim PSheet As Worksheet
Dim DSheet As Worksheet
Dim PCache As PivotCache
Dim PTable As PivotTable
Dim PRange As Range
Dim LastRow As Long
Dim LastCol As Long
On Error Resume Next
Application.DisplayAlerts = False
Worksheets("PivotTable").Delete
Sheets.Add Before:=ActiveSheet
ActiveSheet.Name = "PivotTable"
Application.DisplayAlerts = True
Set PSheet = Worksheets("PivotTable")
Set DSheet = Worksheets("Sheet1")
LastRow = DSheet.Cells(Rows.Count, 1).End(x1Up).Row
LastCol = DSheet.Cells(1, Columns.Count).End(x1ToLeft).Column
Set PRange = DSheet.Cells(1, 1).Resize(LastRow, LastCol)
Set PCache = ActiveWorkbook.PivotCaches.Create _
(SourceType:=x1Database, SourceData:=PRange). _
CreatePivotTable(TableDestination:=PSheet.Cells(2, 2), _
TableName:="Pivot")
Set PTable = PCache.CreatePivotTable _
(TableDestination:=PSheet.Cells(1, 1), TableName:="Pivot")
With ActiveSheet.PivotTables("Pivot").PivotFields("P-AC")
.Orientation = x1RowField
.Position = 1
End With
With ActiveSheet.PivotTables("Pivot").PivotFields("P-Callback")
.Orientation = x1RowField
.Position = 2
End With
With ActiveSheet.PivotTables("Pivot").PivotFields("p-Email")
.Orientation = x1RowField
.Position = 3
End With
With ActiveSheet.PivotTables("Pivot").PivotFields("P-Research")
.Orientation = x1RowField
.Position = 4
End With
ActiveSheet.PivotTables("Pivot").ShowTableStyleRowStripes = True
ActiveSheet.PivotTables("Pivot").TableStyle2 = "PivotStyleMedium9"
End Sub
My source data is formulated as follows:
A | B | C | D | E | F | G | H | I | J |
Agent Name | *Total Calls | Unscheduled Break | *Lunch Time | *Break Time | P-AC | P-Callback | P-Email | P-Research | Wrap TOTAL |
John | 57 | 00:15:32 | 00:30:38 | 00:40:58 | 00:01:48 | 00:21:32 | 00:07:03 | 00:00:00 | 01:26:32 |
George | 44 | 00:16:42 | 00:29:53 | 00:32:29 | 00:00:00 | 00:30:05 | 01:04:25 | 00:00:00 | 00:52:56 |
Paul | 34 | 00:03:33 | 00:29:42 | 00:31:47 | 00:00:00 | 00:00:00 | 01:25:01 | 00:00:00 | 01:29:10 |
Ringo | 35 | 00:01:37 | 00:31:47 | 00:32:52 | 00:00:00 | 02:25:21 | 00:00:00 | 00:00:26 | 00:53:28 |
Sub PIVOT()
Dim PSheet As Worksheet
Dim DSheet As Worksheet
Dim PCache As PivotCache
Dim PTable As PivotTable
Dim PRange As Range
Dim LastRow As Long
Dim LastCol As Long
On Error Resume Next
Application.DisplayAlerts = False
Worksheets("PivotTable").Delete
Sheets.Add Before:=ActiveSheet
ActiveSheet.Name = "PivotTable"
Application.DisplayAlerts = True
Set PSheet = Worksheets("PivotTable")
Set DSheet = Worksheets("Sheet1")
LastRow = DSheet.Cells(Rows.Count, 1).End(x1Up).Row
LastCol = DSheet.Cells(1, Columns.Count).End(x1ToLeft).Column
Set PRange = DSheet.Cells(1, 1).Resize(LastRow, LastCol)
Set PCache = ActiveWorkbook.PivotCaches.Create _
(SourceType:=x1Database, SourceData:=PRange). _
CreatePivotTable(TableDestination:=PSheet.Cells(2, 2), _
TableName:="Pivot")
Set PTable = PCache.CreatePivotTable _
(TableDestination:=PSheet.Cells(1, 1), TableName:="Pivot")
With ActiveSheet.PivotTables("Pivot").PivotFields("P-AC")
.Orientation = x1RowField
.Position = 1
End With
With ActiveSheet.PivotTables("Pivot").PivotFields("P-Callback")
.Orientation = x1RowField
.Position = 2
End With
With ActiveSheet.PivotTables("Pivot").PivotFields("p-Email")
.Orientation = x1RowField
.Position = 3
End With
With ActiveSheet.PivotTables("Pivot").PivotFields("P-Research")
.Orientation = x1RowField
.Position = 4
End With
ActiveSheet.PivotTables("Pivot").ShowTableStyleRowStripes = True
ActiveSheet.PivotTables("Pivot").TableStyle2 = "PivotStyleMedium9"
End Sub