Hello,
I had followed along with the code in this link Automate Your Pivot Table With VBA [Step By Step Guide] and edited the code to match my data and I can't seem to get it to work. It currently runs thru with no errors but also just creates the PivotTable Sheet and then just leaves the sheet blank.
Here is my code:
Here is what I would like the Pivot Table to look like:
I had followed along with the code in this link Automate Your Pivot Table With VBA [Step By Step Guide] and edited the code to match my data and I can't seem to get it to work. It currently runs thru with no errors but also just creates the PivotTable Sheet and then just leaves the sheet blank.
Here is my code:
VBA Code:
Sub PT()
'Declare Variables'
Dim pws As Worksheet
Dim tws 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 pws = Worksheets("PivotTable")
Set tws = Worksheets("Transactions")
'Define Data Range'
LastRow = tws.Cells(Rows.Count, 1).End(xlUp).Row
LastCol = tws.Cells(1, Columns.Count).End(xlToLeft).Column
Set PRange = tws.Cells(1, 1).Resize(LastRow, LastCol)
'Define Pivot Cache'
Set PCache = ActiveWorkbook.PivotCaches.Create _
(SourceType:=xlDatabase, SourceData:=PRange). _
CreatePivotTable(TableDestination:=PSheet.Cells(2, 2), _
TableName:="Table1")
'Insert Blank Pivot Table'
Set PTable = PCache.CreatePivotTable _
(TableDestination:=pws.Cells(1, 1), TableName:="Table1")
'Insert Row Fields'
With ActiveSheet.PivotTables("Table1").PivotFields("RSQM_Cust_Name")
.Orientation = xlRowField
.Position = 1
End With
'Insert Data Field'
With ActiveSheet.PivotTables("Table1").PivotFields("Equipment ID")
.Orientation = xlDataField
.Position = 1
.Function = xlCount
.Name = "Device Count"
End With
With ActiveSheet.PivotTables("Table1").PivotFields("BDS Unit Price")
.Orientation = xlDataField
.Position = 2
.Function = xlSum
.NumberFormat = "#,##0"
.Name = "Sum of BDS Unit Price"
End With
With ActiveSheet.PivotTables("Table1").PivotFields("FY22 Impact")
.Orientation = xlDataField
.Position = 3
.Function = xlSum
.NumberFormat = "#,##0"
.Name = "Sum of FY22 Impact"
End With
'Format Pivot'
TableActiveSheet.PivotTables("Table1").ShowTableStyleRowStripes = TrueActiveSheet.PivotTables("Table1").TableStyle2 = "PivotStyleLight16"
End Sub
Here is what I would like the Pivot Table to look like:
FY22 Q2 Q3 (Jul-Dec 21) UNC Hospital Pricing Doc - SM Review.xlsx | ||||||
---|---|---|---|---|---|---|
A | B | C | D | |||
3 | Row Labels | Sum of BDS Unit Price | Sum of FY22 Impact | Device Count | ||
4 | UNC Hospitals-CTS | $ 50,718.00 | $ 58,355.03 | 4746 | ||
5 | UNC Rockingham Healthcare | $ (2,017.48) | $ 7,473.66 | 423 | ||
6 | UNC P & A Clinics-CTS | $ (78,699.21) | $ (53,855.30) | 398 | ||
7 | UNC Physician Network | $ (12,513.85) | $ (9,480.65) | 306 | ||
8 | Chatham Hospital-CTS | $ 154.17 | $ 2,068.03 | 132 | ||
9 | UNC Campus Health Services-CTS | $ (25,935.97) | $ (22,876.72) | 105 | ||
10 | UNC DIABETES CARE CENTER | $ - | $ - | 2 | ||
11 | Grand Total | $ (68,294.34) | $ (18,315.96) | 6112 | ||
Pivot |