Pivot Table VBA

motopdx

New Member
Joined
Feb 24, 2020
Messages
13
Office Version
  1. 365
Platform
  1. Windows
I have a VBA code to clean up a report which summarizes the data in a pivot table. I only have rows & multiple values but when the macro is ran it only includes the rows. Some reason the values are not pulling in. Code below

'Insert Pivot Table
'Declare Variables
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

'Insert a New Blank Worksheet
On Error Resume Next
Application.DisplayAlerts = False
Worksheets("Pivot").Delete
Sheets.Add Before:=ActiveSheet
ActiveSheet.Name = "Pivot"
Application.DisplayAlerts = True
Set PSheet = Worksheets("Pivot")
Set DSheet = Worksheets("Data")

'Define Data Range
LastRow = DSheet.Cells(Rows.Count, 1).End(xlUp).Row
LastCol = DSheet.Cells(1, Columns.Count).End(xlToLeft).Column
Set PRange = DSheet.Cells(1, 1).Resize(LastRow, LastCol)

'Define Pivot Cache
Set PCache = ActiveWorkbook.PivotCaches.Create _
(SourceType:=xlDatabase, SourceData:=PRange)

'CreatePivotTable(TableDestination:=PSheet.Cells(1, 1), _
TableName:="SalesPivotTable")

'Insert Blank Pivot Table
Set PTable = PCache.CreatePivotTable _
(TableDestination:=PSheet.Cells(1, 1), TableName:="SalesPivotTable")

'Insert Row Fields
With PTable.PivotFields("Transaction Date Time")
.Orientation = xlRowField
.Position = 1
End With

'Insert Data Field
With PTable.PivotTables("SalesPivotTable")
.PivotFields ("Gross Revenue")
.Orientation = xlDataField
.Position = 1
.Description = "Sum of Gross Revenue"
.Function = xlSum
.NumberFormat = "#,##0"
.Name = "Revenue "
End With

With PTable.PivotTables("SalesPivotTable")
.PivotFields ("Refunds")
.Orientation = xlDataField
.Position = 2
.Description = "Sum of Refunds"
.Function = xlSum
.NumberFormat = "#,##0"
.Name = "Refunds "
End With


'Format Pivot Table
ActiveSheet.PTable("SalesPivotTable").ShowTableStyleRowStripes = True

This is what I get:
1610682673042.png





This is what is should look like:


1610682583739.png
1610682619715.png


Thank you in advance for any help
 

Some videos you may like

Excel Facts

What does custom number format of ;;; mean?
Three semi-colons will hide the value in the cell. Although most people use white font instead.

mart37

Well-known Member
Joined
Aug 4, 2017
Messages
755
Office Version
  1. 2016
Platform
  1. Windows
With PTable.PivotTables("SalesPivotTable")

With PTable
 

mart37

Well-known Member
Joined
Aug 4, 2017
Messages
755
Office Version
  1. 2016
Platform
  1. Windows
PTable = PivotTables("SalesPivotTable")

So this is wrong:
'Insert Data Field
With PTable.PivotTables("SalesPivotTable")
.PivotFields ("Gross Revenue")
.Orientation = xlDataField
.Position = 1
.Description = "Sum of Gross Revenue"
.Function = xlSum
.NumberFormat = "#,##0"
.Name = "Revenue "
End With

Other mistake:
With PTable.PivotTables("SalesPivotTable")
.PivotFields ("Gross Revenue")


Replace with:
VBA Code:
'Insert Data Field
With PTable.PivotFields ("Gross Revenue")
.Orientation = xlDataField
.Position = 1
.Description = "Sum of Gross Revenue"
.Function = xlSum
.NumberFormat = "#,##0"
.Name = "Revenue "
End With
 
Solution

motopdx

New Member
Joined
Feb 24, 2020
Messages
13
Office Version
  1. 365
Platform
  1. Windows
Awesome, thank you so much! This worked perfect.
 

Watch MrExcel Video

Forum statistics

Threads
1,123,281
Messages
5,600,719
Members
414,401
Latest member
grenona2020

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Top