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
Related
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
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
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
I run the same reports every day and would like to use a generic VBA code that selects all my data and creates the pivot table. I've recorded the MACRO:
I need the data range selected automatically to be allow any data amount
I need the data range to be selected automatically
The sheet should not matter
Finally I would like to name the sheet
code:
Sheets.Add
ActiveWorkbook.Worksheets("Sheet1").PivotTables("PivotTable26").PivotCache. _
CreatePivotTable TableDestination:="Sheet4!R3C1", TableName:="PivotTable27" _
, DefaultVersion:=xlPivotTableVersion15
Sheets("Sheet4").Select
Cells(3, 1).Select
With ActiveSheet.PivotTables("PivotTable27").PivotFields("Date Opened")
.Orientation = xlRowField
.Position = 1
End With
With ActiveSheet.PivotTables("PivotTable27").PivotFields("Date Opened")
.Orientation = xlColumnField
.Position = 1
End With
With ActiveSheet.PivotTables("PivotTable27").PivotFields("Queue")
.Orientation = xlRowField
.Position = 1
End With
With ActiveSheet.PivotTables("PivotTable27").PivotFields("Sub Queue")
.Orientation = xlRowField
.Position = 2
End With
With ActiveSheet.PivotTables("PivotTable27").PivotFields("Status")
.Orientation = xlColumnField
.Position = 1
End With
ActiveSheet.PivotTables("PivotTable27").AddDataField ActiveSheet.PivotTables( _
"PivotTable27").PivotFields("Source"), "Count of Source", xlCount
With ActiveSheet.PivotTables("PivotTable27").PivotFields("Queue")
.PivotItems("(blank)").Visible = False
End With
End Sub
Here's a little code to get you what you asked in your post:
Option Explicit
Sub AutoPivot()
Dim PT As PivotTable
Dim PTCache As PivotCache
Dim PtDataRng As Range
Dim ws As Worksheet
Dim LastRow As Long
Dim LastCol As Long
Set ws = Worksheets("PivotData") ' <-- modify "PivotData" to your sheet's name that holds the dynamic data
With ws
LastCol = .Cells(1, .Columns.Count).End(xlToLeft).Column ' <-- get last column with data in row 1
LastRow = .Cells(.Rows.Count, "A").End(xlToLeft).Row ' <-- get last row with data in column A
Set PtDataRng = .Range(.Cells(1, 1), .Cells(LastRow, LastCol)) ' <-- set the dynamic Range
End With
' create a new wroksheet, place it at the end, and use an InputBox to select it's name
Set ws = ThisWorkbook.Sheets.Add(after:=Sheets(ThisWorkbook.Sheets.Count))
ws.Name = Application.InputBox("Select A Valid Name for new sheet", Type:=2)
' set the Pivot Cache to updated dynamic Ranget
Set PTCache = ThisWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:=PtDataRng)
' Create Pivot Table from Pivot Cache
Set PT = ws.PivotTables.Add(PivotCache:=PTCache, TableDestination:=ws.Range("A3"), TableName:="PivotTableTest")
' set the fields setting in the Pivot Table
With PT
With .PivotFields("Date Opened")
.Orientation = xlRowField
.Position = 1
End With
With .PivotFields("Date Opened")
.Orientation = xlColumnField
.Position = 1
End With
With .PivotFields("Queue")
.Orientation = xlRowField
.Position = 1
End With
With .PivotFields("Sub Queue")
.Orientation = xlRowField
.Position = 2
End With
With .PivotFields("Status")
.Orientation = xlColumnField
.Position = 1
End With
.AddDataField .PivotFields("Source"), "Count of Source", xlCount
With .PivotFields("Queue")
.PivotItems("(blank)").Visible = False
End With
End With
End Sub
I'm trying to create a Pivot table, but getting Invalid Procedure Call or Argument.
ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:="rng", Version:=xlPivotTableVersion14).CreatePivotTable TableDestination:="rngB", TableName:="pvtReportA_B", DefaultVersion:=xlPivotTableVersion14
rng (The source) is a range consisting of about 20 columns and a few thousand rows.
rngB (The destination) is a single cell in a different worksheet
Can anyone advise where I am going wrong?
EDIT:
My fault, I should have been using rngData and not rng as the Source.
Set rng = wsA.Range("C14")
Set rngData = Range(rng, rng.End(xlToRight))
Set rngData = Range(rng, rng.End(xlDown))
Set rngB = wsB.Range("C8")
ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:=rngData, Version:=xlPivotTableVersion14).CreatePivotTable TableDestination:=rngB, TableName:="pvtReportA_B", DefaultVersion:=xlPivotTableVersion14
This brings up the PivotTable frame just fine.
In this instance, I used the wrong range object, which caused Excel to throw a fit.
Set rng = wsA.Range("C14")
Set rngData = Range(rng, rng.End(xlToRight))
Set rngData = Range(rng, rng.End(xlDown))
Set rngB = wsB.Range("C8")
ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:=rngData, Version:=xlPivotTableVersion14).CreatePivotTable TableDestination:=rngB, TableName:="pvtReportA_B", DefaultVersion:=xlPivotTableVersion14
To create a pivot in Excel 2010, using VBA code, you can use and adapt this template:
Sub newPVT()
Dim PTCache As PivotCache
Dim PT As PivotTable
'Create the Cache
Set PTCache = ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, _
SourceData:=Range("Dynamic_Field_Summary"))
'Select the destination sheet
Sheets("Field Summary").Select
'Create the Pivot table
Set PT = ActiveSheet.PivotTables.Add(PivotCache:=PTCache, _
TableDestination:=Range("P1"), TableName:="Pivot1")
ActiveWorkbook.ShowPivotTableFieldList = True
'Adding fields
With PT
With .PivotFields("Enterprise")
.Orientation = xlColumnField
.Position = 1
End With
With .PivotFields("Field")
.Orientation = xlRowField
.Position = 1
End With
With .PivotFields("Planted Acres")
.Orientation = xlDataField
.Position = 1
.Caption = " Planted Acres"
.Function = xlSum
End With
With .PivotFields("Harvested Acres")
.Orientation = xlDataField
.Position = 2
.Caption = " Harvested Acres"
.Function = xlSum
End With
With .PivotFields("lbs")
.Orientation = xlDataField
.Position = 3
.Caption = " lbs"
.Function = xlSum
End With
'Adjusting some settings
.RowGrand = False
.DisplayFieldCaptions = False
.HasAutoFormat = False
'Improving the layout
.TableStyle2 = "PivotStyleMedium9"
.ShowTableStyleRowStripes = True
.ShowTableStyleColumnStripes = True
End With
With ActiveSheet
'Adjusting columns width
.Columns("P:V").ColumnWidth = 16
.Range("Q2:V2").HorizontalAlignment = xlCenter
End With
ActiveWorkbook.ShowPivotTableFieldList = False
End Sub
I found it here.
In this page you can also fine the meaning of every part of the code, for example is explained here.
I think that this is a good code also to start creating vba macros for Excel 2007 or other versions.