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
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 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 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
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 got this Run-time error '1004':- This command requires at least two rows of source data. You cannot use the command on a selection in only one row. Try the following: If you're using an advanced filter, select a range of cells that contains at least two rows of data. Then click the Advanced Filter command again. If you're creating a PivotTable report or PivotChart report, type a [it stops here]
I got the aforementioned error at:-
pvc.CreatePivotTable TableDestination:=Worksheets("TABLE").Range("A1"), _
TableName:="INFO", DefaultVersion:=xlPivotTableVersion12
I'm trying to run this code:-
Sub CreateTable()
Dim lastRow As Long
Dim pvc As PivotCache
lastRow = ActiveSheet.Cells(Rows.Count, 6).End(xlUp).Row
ActiveWorkbook.Names.Add Name:="Database", RefersTo:="=DATA!$G$1:$K$" & lastRow
Set pvc = ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
"Database", Version:=xlPivotTableVersion12)
pvc.CreatePivotTable TableDestination:=Worksheets("TABLE").Range("A1"), _
TableName:="INFO", DefaultVersion:=xlPivotTableVersion12
Sheets("TABLE").Select
Cells(1, 1).Select
ActiveWorkbook.ShowPivotTableFieldList = True
With ActiveSheet.PivotTables("INFO").PivotFields("MODEL")
.Orientation = xlRowField
.Position = 1
End With
With ActiveSheet.PivotTables("INFO").PivotFields("TYPE")
.Orientation = xlRowField
.Position = 2
End With
ActiveSheet.PivotTables("INFO").AddDataField ActiveSheet.PivotTables( _
"INFO").PivotFields("GRADE"), "Sum of GRADE", xlSum
ActiveSheet.PivotTables("INFO").AddDataField ActiveSheet.PivotTables( _
"INFO").PivotFields("SIZE"), "Sum of SIZE", xlSum
ActiveSheet.PivotTables("INFO").AddDataField ActiveSheet.PivotTables( _
"INFO").PivotFields("QTY"), "Sum of QTY", xlSum
With ActiveSheet.PivotTables("INFO").PivotFields("MODEL")
.Orientation = xlColumnField
.Position = 2
End With
With ActiveSheet.PivotTables("INFO").PivotFields("TYPE")
.Orientation = xlColumnField
.Position = 3
End With
With ActiveSheet.PivotTables("INFO").PivotFields("Sum of GRADE")
.Orientation = xlPINFOField
.Position = 1
End With
With ActiveSheet.PivotTables("INFO").PivotFields("Sum of SIZE")
.Orientation = xlRowField
.Position = 1
End With
ActiveWorkbook.ShowPivotTableFieldList = False
Range("B3").Select
ActiveSheet.PivotTables("INFO").CompactLayoutColumnHeader = "MODEL"
Range("A5").Select
ActiveSheet.PivotTables("INFO").CompactLayoutRowHeader = "SIZE"
Range("A1").Select
ActiveSheet.PivotTables("INFO").PivotFields("GRADE").Caption = "GRADE"
Cells.Select
Cells.EntireColumn.AutoFit
Columns("B:BB").Select
Selection.Style = "Comma"
Cells.Select
Cells.EntireColumn.AutoFit
Range("C1").Select
End Sub
Pls help.
Thanks.
It appears the reason that this error occurs is due to the fourth row:
lastRow = ActiveSheet.Cells(Rows.Count, 6).End(xlUp).Row
by the look of this, you are finding the last row of data in your "DATA" sheet by looking in column 6. Now if we look what you are combining this with:
ActiveWorkbook.Names.Add Name:="Database", RefersTo:="=DATA!$G$1:$K$" & lastRow
Now, if your source data is in cells G1:K** then:
ActiveSheet.Cells(Rows.Count, 6).End(xlUp).Row
will always return 1 unless you have data in column 6, but from the line above, it seems that your data starts in column 7 ("G"). Therefore, to get this to work you have to change the fourth row to:
lastRow = ActiveSheet.Cells(Rows.Count, 7).End(xlUp).Row