VBA Create Multi Pivot Table - excel

Code was running with no error pop-out, but no pivot table is showing in tab Apple# and Banana#.There must be something wrong.
Hope someone could check my code.. or teach me a better way to do this.
Sub TableTesting()
Dim PAppleSheet, PBananaSheet As Worksheet
Dim DAppleSheet, DBananaSheet As Worksheet
Dim AppleCache1, AppleCache2, BananaCache1, BananaCache2 As PivotCache
Dim AppleTable1, AppleTable2, BananaTable1, BananaTable2 As PivotTable
Dim PAppleRange, PBananaRange As Range
Dim LastRowApple, LastRowBanana As Long
Dim LastColApple, LastColBanana As Long
'Apple
On Error Resume Next
Set PAppleSheet = Worksheets("Apple#")
Set DAppleSheet = Worksheets("Apple")
LastRowApple = DAppleSheet.Cells(Rows.Count, 1).End(xlUp).Row
LastColApple = DAppleSheet.Cells(1, Columns.Count).End(xlToLeft).Column
Set PAppleRange = DAppleSheet.Cells(1, 1).Resize(LastRow, LastCol)
Set AppleCache1 = ActiveWorkbook.PivotCaches.Create _
(SourceType:=xlDatabase, SourceData:=PAppleRange). _
CreatePivotTable(TableDestination:=PAppleSheet.Cells(1, 1), _
TableName:="AppleSTRTable1")
Set AppleTable1 = AppleCache1.CreatePivotTable _
(TableDestination:=PAppleSheet.Cells(1, 1), TableName:="AppleSTRTable1")
With ActiveSheet.PivotTables("AppleSTRTable1").PivotFields("Sender")
.Orientation = xlRowField
.Position = 1
End With
With ActiveSheet.PivotTables("AppleSTRTable1").PivotFields("Amount")
.Orientation = xlDataField
.Position = 1
.Function = xlSum
.NumberFormat = "#,##0"
.Name = "Amount "
End With
With ActiveSheet.PivotTables("AppleSTRTable1").PivotFields("Amount")
.Orientation = xlDataField
.Position = 1
.Function = xlCount
.NumberFormat = "#,##0"
.Name = "Item "
End With
Set AppleCache2 = ActiveWorkbook.PivotCaches.Create _
(SourceType:=xlDatabase, SourceData:=PAppleRange). _
CreatePivotTable(TableDestination:=PAppleSheet.Cells(1, 6), _
TableName:="AppleSTRTable2")
Set AppleTable2 = AppleCache2.CreatePivotTable _
(TableDestination:=PAppleSheet.Cells(1, 1), TableName:="AppleSTRTable2")
With ActiveSheet.PivotTables("AppleSTRTable2").PivotFields("Sender")
.Orientation = xlRowField
.Position = 1
End With
With ActiveSheet.PivotTables("AppleSTRTable2").PivotFields("Color")
.Orientation = xlRowField
.Position = 2
End With
With ActiveSheet.PivotTables("AppleSTRTable2").PivotFields("Amount")
.Orientation = xlDataField
.Position = 1
.Function = xlSum
.NumberFormat = "#,##0"
.Name = "Amount "
End With
With ActiveSheet.PivotTables("AppleSTRTable2").PivotFields("Amount")
.Orientation = xlDataField
.Position = 1
.Function = xlCount
.NumberFormat = "#,##0"
.Name = "Item "
End With
'Banana
On Error Resume Next
Set PBananaSheet = Worksheets("Banana#")
Set DBananaSheet = Worksheets("Banana")
LastRowBanana = DBananaSheet.Cells(Rows.Count, 1).End(xlUp).Row
LastColBanana = DBananaSheet.Cells(1, Columns.Count).End(xlToLeft).Column
Set PBananaRange = DBananaSheet.Cells(1, 1).Resize(LastRow, LastCol)
Set BananaCache1 = ActiveWorkbook.PivotCaches.Create _
(SourceType:=xlDatabase, SourceData:=PBananaRange). _
CreatePivotTable(TableDestination:=PBananaSheet.Cells(1, 1), _
TableName:="BananaSTRTable1")
Set BananaTable1 = BananaCache1.CreatePivotTable _
(TableDestination:=PBananaSheet.Cells(1, 1), TableName:="BananaSTRTable1")
With ActiveSheet.PivotTables("BananaSTRTable1").PivotFields("Sender")
.Orientation = xlRowField
.Position = 1
End With
With ActiveSheet.PivotTables("BananaSTRTable1").PivotFields("Amount")
.Orientation = xlDataField
.Position = 1
.Function = xlSum
.NumberFormat = "#,##0"
.Name = "Amount "
End With
With ActiveSheet.PivotTables("BananaSTRTable1").PivotFields("Amount")
.Orientation = xlDataField
.Position = 1
.Function = xlCount
.NumberFormat = "#,##0"
.Name = "Item "
End With
Set BananaCache2 = ActiveWorkbook.PivotCaches.Create _
(SourceType:=xlDatabase, SourceData:=PBananaRange). _
CreatePivotTable(TableDestination:=PBananaSheet.Cells(1, 6), _
TableName:="BananaSTRTable2")
Set BananaTable2 = BananaCache2.CreatePivotTable _
(TableDestination:=PBananaSheet.Cells(1, 1), TableName:="BananaSTRTable2")
With ActiveSheet.PivotTables("BananaSTRTable2").PivotFields("Sender")
.Orientation = xlRowField
.Position = 1
End With
With ActiveSheet.PivotTables("BananaSTRTable2").PivotFields("Color")
.Orientation = xlRowField
.Position = 2
End With
With ActiveSheet.PivotTables("BananaSTRTable2").PivotFields("Amount")
.Orientation = xlDataField
.Position = 1
.Function = xlSum
.NumberFormat = "#,##0"
.Name = "Amount "
End With
With ActiveSheet.PivotTables("BananaSTRTable2").PivotFields("Amount")
.Orientation = xlDataField
.Position = 1
.Function = xlCount
.NumberFormat = "#,##0"
.Name = "Item "
End With
End Sub

Related

Fetching only 5 columns from 111 columns in pivot table in excel vba

I have a pivot table with 111 headers or fields and i am trying to create a pivot table with 1 header as row field and 5 headers as value fields.
I am able to fetch 1 row field but for value fields i am able to fetch 1 field only and remaining 4 fields are not fetching.
Please note headers are in row 14.
I tried below code until now.
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
Set PSheet = Worksheets("PivotTable")
Set DSheet = Worksheets("Options")
LastRow = DSheet.Cells(Rows.Count, 1).End(xlUp).Row
LastCol = DSheet.Cells(14, Columns.Count).End(xlToLeft).Column
Set PRange = DSheet.Cells(14, 1).Resize(LastRow, LastCol)
Set PCache = ActiveWorkbook.PivotCaches.Create _
(SourceType:=xlDatabase, SourceData:=PRange). _
CreatePivotTable(TableDestination:=PSheet.Cells(2, 1), _
TableName:="PivotTable")
Set PTable = PCache.CreatePivotTable _
(TableDestination:=PSheet.Cells(1, 1), TableName:="PivotTable")
With ActiveSheet.PivotTables("PivotTable").PivotFields("Client")
.Orientation = xlRowField
.Position = 1
End With
With ActiveSheet.PivotTables("PivotTable").PivotFields("Final Base")
.Orientation = xlDataField
.Function = xlSum
.Position = 1
.NumberFormat = "#,##0"
End With
With ActiveSheet.PivotTables("PivotTable").PivotFields("Sum of Green")
.Orientation = xlDataField
.Function = xlSum
.Position = 2
End With
With ActiveSheet.PivotTables("PivotTable").PivotFields("Sum of Amber)")
.Orientation = xlDataField
.Function = xlSum
.Position = 3
End With
With ActiveSheet.PivotTables("PivotTable").addPivotFields("Sum of Toll)")
.Orientation = xlDataField
.Function = xlSum
.Position = 4
End With
With ActiveSheet.PivotTables("PivotTable").PivotFields("Sum of Red)")
.Orientation = xlDataField
.Function = xlSum
.Position = 5
End With
ActiveSheet.PivotTables("PivotTable").ShowTableStyleRowStripes = True
ActiveSheet.PivotTables("PivotTable").TableStyle2 = "PivotStyleMedium9"
End Sub

Choose positions in the pivot table

I have a macro that creates a PivotTable for me with the fields I want. However, I would like them to choose the indicated values.This is what the table looks like at the start
here is what I would like e.g.
this is my code:
Set sh_s = wbMe.Sheets("kredyty")
lr1 = sh_s.Range("A" & Rows.Count).End(xlUp).Row
ptver1 = 6
tbName1 = "Tabela przestawna"
Sheets.Add after:=Sheets(Sheets.Count)
Set sh_d = ActiveSheet
ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, _
SourceData:=sh_s.Name & "!R1C1:R" & lr1 & "C58", Version:=ptver).CreatePivotTable _
TableDestination:=sh_d.Name & "!R3C1", TableName:=tbName1, _
DefaultVersion:=ptver1
With sh_d.PivotTables(tbName1)
With .PivotFields("CRD_RWG")
.Orientation = xlPageField
.Position = 1
With sh_d.PivotTables(tbName1)
With .PivotFields("NEW_DEFAULT")
.Orientation = xlPageField
.Position = 1
End With
.AddDataField .PivotFields("CRD_EKSP_PIER_DC_FIN"), "Suma z Ekspozycji", xlSum
.AddDataField .PivotFields("CRD_KOR_DC_FIN"), "Suma z Korekty", xlSum
End With

VBA Application definied error in pivot table

I have a PivotTable that selects items like xlPageField and PivotField, but I wish it would automatically select options for me in those filters. I wanted to add
.CurrentPage = 1
But i got error there about Application definied or object error. I tried change to .CurrentPageName but not helped.
This is my part of code :
With sh_d.PivotTables(tbName1)
With .PivotFields("CRD_RWG")
.Orientation = xlPageField
.Position = 1
.CurrentPage = 1
With sh_d.PivotTables(tbName1)
With .PivotFields("NEW_DEFAULT")
.Orientation = xlPageField
.Position = 1
.CurrentPageName = Y
Solutions is a below :
Dim wbMe As Workbook
Dim sh_s As Worksheet, sh_d As Worksheet
Set wbMe = ActiveWorkbook
Set sh_s = wbMe.Sheets("kredyty")
lr1 = sh_s.Cells(Rows.Count, "A").End(xlUp).Row
ptver1 = 6
tbName1 = "Tabela przestawna"
Sheets.Add after:=Sheets(Sheets.Count)
Set sh_d = ActiveSheet
ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, _
SourceData:=sh_s.Name & "!R1C1:R" & lr1 & "C12", Version:=ptver1).CreatePivotTable _
TableDestination:=sh_d.Name & "!R3C1", TableName:=tbName1, _
DefaultVersion:=ptver1
With sh_d.PivotTables(tbName1).PivotFields("CRD_RWG")
.Orientation = xlPageField
.Position = 1
.ClearAllFilters
.CurrentPage = 1
End With
With sh_d.PivotTables(tbName1).PivotFields("NEW_DEFAULT")
.Orientation = xlRowField
.Position = 1
End With
With sh_d.PivotTables(tbName1)
.AddDataField .PivotFields("CRD_EKSP_PIER_DC_FIN"), "Suma z Ekspozycji", xlSum
.AddDataField .PivotFields("CRD_KOR_DC_FIN"), "Suma z Korekty", xlSum
End With

Excel VBA adding Values field in Pivot Table not working

Hi I am trying to add a Values field in my Pivot table. Here is the code I have for now :
'Insert Row Fields
With ActiveSheet.PivotTables("PivotTable").PivotFields("Account Lvl1 Desc")
.Orientation = xlRowField
.Position = 1
End With
With ActiveSheet.PivotTables("PivotTable").PivotFields("Account Description")
.Orientation = xlRowField
.Position = 2
End With
With ActiveSheet.PivotTables("PivotTable").PivotFields("Supplier")
.Orientation = xlRowField
.Position = 3
End With
'Insert Data Field
With ActiveSheet.PivotTables("PivotTable")
.PivotFields ("Distrib Amt CAD")
.Orientation = xlDataField
.Function = xlSum
'.NumberFormat = "#,##0"
.Name = "Sum of OH in CAD "
End With
Everything else is working but the Values
I would recommend some clean-up:
With ActiveSheet.PivotTables("PivotTable")
'Insert Row Fields
With .PivotFields("Account Lvl1 Desc")
.Orientation = xlRowField
.Position = 1
End With
With .PivotFields("Account Description")
.Orientation = xlRowField
.Position = 2
End With
With .PivotFields("Supplier")
.Orientation = xlRowField
.Position = 3
End With
'Insert Data Field
With .PivotFields ("Distrib Amt CAD")
.Orientation = xlDataField
.Function = xlSum
'.NumberFormat = "#,##0"
.Name = "Sum of OH in CAD "
End With
End With
If you have values that you, say, exported from SharePoint field tables, those are strings... run a similar code to the below on your data so you can display as a number:
With sws 'source worksheet
lr = .Cells(.Rows.Count, 1).End(xlUp).Row
For i = 2 To lr
.Cells(i, 6).Value = Val(.Cells(i, 6).Value) * 1 'Uses column 6 (F)
Next i
End With
Once the appropriate data exists, it will give more than #Value errors on the output.

Excel Macro- How to set a fixed name to the pivot chart?

The below is the code that I have used. I want to fix my chart numbers of this and the other charts that I have created so that I can format them later. Also if someone can suggest how to shorten up the code.
'Declare Variables
Dim PCache7 As PivotCache
Dim PTable7 As PivotTable
'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 PCache7 = ActiveWorkbook.PivotCaches.Create _
(SourceType:=xlDatabase, SourceData:=PRange). _
CreatePivotTable(TableDestination:=PSheet.Cells(2, 12), _
TableName:="AnalyticsPivotTable7")
'Insert Blank Pivot Table
Set PTable7 = PCache.CreatePivotTable _
(TableDestination:=PSheet.Cells(2, 12), TableName:="AnalyticsPivotTable7")
'Insert Row Fields
With ActiveSheet.PivotTables("AnalyticsPivotTable7").PivotFields("Issue Type")
.Orientation = xlRowField
.Position = 1
End With
'Insert Data Field
With ActiveSheet.PivotTables("AnalyticsPivotTable7").PivotFields("IncidentID")
.Orientation = xlDataField
.Position = 1
.Function = xlCount
.NumberFormat = "#,##0"
End With
'Insert Data Field
With ActiveSheet.PivotTables("AnalyticsPivotTable7").PivotFields("IncidentID")
.Orientation = xlDataField
.Position = 2
.Function = xlCount
.Calculation = xlPercentOfTotal
.NumberFormat = "#0.0%"
End With
'Format Pivot Table
ActiveSheet.PivotTables("AnalyticsPivotTable7").ShowTableStyleRowStripes = True
ActiveSheet.PivotTables("AnalyticsPivotTable7").TableStyle2 = "PivotStyleMedium9"
Range("N4:N16").Select
ActiveSheet.PivotTables("AnalyticsPivotTable7").PivotFields("Issue Type"). _
AutoSort xlDescending, "Count of IncidentID2", ActiveSheet.PivotTables( _
"AnalyticsPivotTable7").PivotColumnAxis.PivotLines(2), 1

Resources