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.
Related
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
The code does not cause an error and a table with the correct row and column fields are created. Nothing appears in the value field though.
'Insert Row Fields
With ActiveSheet.PivotTables("Rev by Facility")
With .PivotFields("Facility")
.Orientation = xlRowField
.Position = 1
End With
'Insert Column Fields
With .PivotFields("Revenue")
.Orientation = xlColumnField
.Position = 1
End With
'Insert Data Field
With .PivotFields("SubTotal 2")
.Orientation = xlValuesField
.Position = 1
.Function = xlSum
.Name = "Sum of SubTotal 2"
End With
End With
This is only part of the code, the above code should not cause the value field to not appear. The data source of the pivot table includes the column and all the data I want to appear in the value field.
Not having your lines properly indented can cause all sorts of issues. Try this
'Insert Row Fields
With ActiveSheet.PivotTables("Rev by Facility")
With .PivotFields("Facility")
.Orientation = xlRowField
.Position = 1
End With
'Insert Column Fields
With .PivotFields("Revenue")
.Orientation = xlColumnField
.Position = 1
End With
'Insert Data Field
With .PivotFields("SubTotal 2")
.Orientation = xlValuesField
.Position = 1
.Function = xlSum
.Name = "Sum of SubTotal 2"
End With
End With
To define a pivot field as "value field", you have to use xlDataField.
.Orientation = xlDataField
Just in case:
The often called "filter fields" have the orientation xlPageField.
This is the recorded macro for creating a pivot table.
While running it I getting an error of:
error 1004 unable to get the pivot tables property of worksheet class
Range("_RallyQueryResultList[#All]").Select
Sheets.Add
ActiveWorkbook.Worksheets("Sheet30").PivotTables("PivotTable8").PivotCache. _
CreatePivotTable TableDestination:="Sheet30!R3C1", TableName:="PivotTable9" _
, DefaultVersion:=xlPivotTableVersion14
Sheets("Sheet30").Select
Cells(3, 1).Select
With ActiveSheet.PivotTables("PivotTable9").PivotFields("Severity")
.Orientation = xlColumnField
.Position = 1
End With
ActiveSheet.PivotTables("PivotTable9").AddDataField ActiveSheet.PivotTables( _
"PivotTable9").PivotFields("Severity"), "Count of Severity", xlCount
With ActiveSheet.PivotTables("PivotTable9").PivotFields("Owner")
.Orientation = xlRowField
.Position = 1
End With
With ActiveSheet.PivotTables("PivotTable9").PivotFields("Requirement")
.Orientation = xlPageField
.Position = 1
End With
End Sub
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