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
 

Excel Facts

How to total the visible cells?
From the first blank cell below a filtered data set, press Alt+=. Instead of SUM, you will get SUBTOTAL(9,)
With PTable.PivotTables("SalesPivotTable")

With PTable
 
Upvote 0
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
 
Upvote 0
Solution

Forum statistics

Threads
1,213,532
Messages
6,114,177
Members
448,554
Latest member
Gleisner2

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
Back
Top