I have a sheet with lot of data, I create a pivot table to have a pie chart, I would like the pie chart to update if I filter on my sourcesheet data. I don't know if there's way to automatically update the pie chart while filtering on the source sheet. For example if I created a pivot table with 10 elements and that I return in my source sheet and apply a new filter on 5 element I would like the pie chart to be updated automatically with the new filter without making a new pivot table
Sub Macro1 ()
Cells.Select
ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
"Alarmes!R1C1:R1048576C11", Version:=xlPivotTableVersion15).CreatePivotTable _
TableDestination:="Graphe DOS!R1C1", TableName:="Tableau croisé dynamique2" _
, DefaultVersion:=xlPivotTableVersion15
Sheets("Graphe DOS").Select
Cells(1, 1).Select
ActiveSheet.Shapes.AddChart2(201, xlColumnClustered).Select
ActiveChart.SetSourceData Source:=Range("'Graphe DOS'!$A$1:$C$18")
With ActiveChart.PivotLayout.PivotTable.PivotFields("Tranches")
.Orientation = xlPageField
.Position = 1
End With
'i'll be filtering on the SE column
With ActiveChart.PivotLayout.PivotTable.PivotFields("SE")
.Orientation = xlPageField
.Position = 1
End With
With ActiveChart.PivotLayout.PivotTable.PivotFields("Alarmes")
.Orientation = xlRowField
.Position = 1
End With
ActiveChart.PivotLayout.PivotTable.AddDataField ActiveChart.PivotLayout. _
PivotTable.PivotFields("Descriptifs"), "Nombre de Descriptifs", xlCount
End Sub
Thank You for your time and help
Related
I think this should be a simple one considering I have very little VB knowledge :) I have recorded a macro to create a pivot table for a range on the current sheet. Of course what this gives me is the code to create a pivot table for that specific sheet. I am trying to adjust the code to grab the name of the active sheet so I can use this macro across different sheets. I have tried to search and find exactly how to retrieve the name and then supply that name to the code that will being the process of creating the pivot table.
I am hitting a roadblock at the ActiveWorkbook.PivotCaches.Create section of the code. I am getting the "Run-time error '1004': Application-defined or object-defined error" message.
Any help would be greatly appreciated!
Sub Macro1()
Dim sht As String
sht = ActiveSheet.Name
Columns("A:K").Select
ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
Worksheets(sht).Range("!R1C1:R1048576C11"), Version:=6).CreatePivotTable TableDestination:= _
Worksheets(sht).Range("!R1C14"), TableName:="PivotTable3", DefaultVersion:=6
ActiveSheet.Select
Cells(1, 14).Select
With ActiveSheet.PivotTables("PivotTable3").PivotFields("Department")
.Orientation = xlRowField
.Position = 1
End With
With ActiveSheet.PivotTables("PivotTable3").PivotFields( _
"Originating Master Name")
.Orientation = xlRowField
.Position = 2
End With
With ActiveSheet.PivotTables("PivotTable3").PivotFields("Net")
.Orientation = xlRowField
.Position = 3
End With
With ActiveSheet.PivotTables("PivotTable3").PivotFields("Month")
.Orientation = xlRowField
.Position = 4
End With
ActiveSheet.PivotTables("PivotTable3").AddDataField ActiveSheet.PivotTables( _
"PivotTable3").PivotFields("Net"), "Count of Net", xlCount
With ActiveSheet.PivotTables("PivotTable3").PivotFields("Month")
.Orientation = xlColumnField
.Position = 1
End With
With ActiveSheet.PivotTables("PivotTable3").PivotFields("Count of Net")
.Caption = "Sum of Net"
.Function = xlSum
End With
End Sub
Change this
Worksheets(sht).Range("!R1C1:R1048576C11")
To this
ActiveWorkbook.Sheets(sht).Range("A1:K1048576")
And this
Worksheets(sht).Range("!R1C14")
To this
ActiveWorkbook.Sheets(sht).Range("N1")
Hi I am facing problem in running macro that captured the pivot table creation. I used excel 2016 to create the macros. VBA Pivot Table Run Time Error 5 Invalid Procedure Call for MS office 2016
From this line i get the error: invalid procedure call or argument
ActiveWorkbook.Worksheets("page2").PivotTables("PivotTable1").PivotCache. _ CreatePivotTable TableDestination:="page3!R3C1", TableName:="PivotTable1" _, DefaultVersion:=6
Sub PIVOT()
'
' PIVOT Macro
'
'
Sheets("page1").Select
Sheets.Add
**ActiveWorkbook.Worksheets("page2").PivotTables("PivotTable1").PivotCache. _
CreatePivotTable TableDestination:="page3!R3C1", TableName:="PivotTable1" _
, DefaultVersion:=6**
Sheets("page3").Select
Cells(3, 1).Select
With ActiveSheet.PivotTables("PivotTable1").PivotFields("fruit")
.Orientation = xlRowField
.Position = 1
End With
ActiveSheet.PivotTables("PivotTable1").AddDataField ActiveSheet.PivotTables( _
"PivotTable1").PivotFields("fruit"), "Count of fruit", xlCount
With ActiveSheet.PivotTables("PivotTable1").PivotFields("description")
.Orientation = xlPageField
.Position = 1
End With
ActiveWindow.SmallScroll Down:=-3
With ActiveSheet.PivotTables("PivotTable1").PivotFields("Actual")
.Orientation = xlColumnField
.Position = 1
End With
ActiveSheet.PivotTables("PivotTable1").PivotFields("description"). _
ClearAllFilters
ActiveSheet.PivotTables("PivotTable1").PivotFields("description"). _
CurrentPage = "group"
Range("B15").Select
End Sub
You could try to change your PivotTableCache version, because if you try to run it in an earlier Excel version send this message.
Somethin like in this post:
Runtime Error 5 Sometimes in VBA Creating PivotTable
Im very new to VBA. I will explain my project.
I have an Excel sheet with a lot of information that I want to filter. For the moment my code looks like this:
With ActiveSheet.PivotTables("Tableau croisé dynamique2").PivotFields( _
"CREATION_DATE")
.Orientation = xlPageField
.Position = 1
End With
ActiveSheet.PivotTables("Tableau croisé dynamique2").AddDataField ActiveSheet. _
PivotTables("Tableau croisé dynamique2").PivotFields("AMOUNT"), _
"Nombre de AMOUNT", xlCount
With ActiveSheet.PivotTables("Tableau croisé dynamique2").PivotFields("STATUS")
.Orientation = xlRowField
.Position = 1
End With
With ActiveSheet.PivotTables("Tableau croisé dynamique2").PivotFields( _
"VENDOR_NAME")
.Orientation = xlRowField
.Position = 2
End With
With ActiveSheet.PivotTables("Tableau croisé dynamique2").PivotFields( _
"INVOICE_NUM")
.Orientation = xlRowField
.Position = 3
End With
With ActiveSheet.PivotTables("Tableau croisé dynamique2").PivotFields( _
"Nombre de AMOUNT")
.Caption = "Somme de AMOUNT"
.Function = xlSum
End With
ActiveSheet.PivotTables("Tableau croisé dynamique2").PivotFields( _
"CREATION_DATE").CurrentPage = "08/09/2016"
This code works to extract to filter with the date 08/09/2016.
Now, I would like to make the pivot table get this date in a cell ahead of launching the code.
But I don't want to have to go in to the code every time to change the date I want.
I would like something like this:
Ex: I write "09/09/2016" in cell "A1"
ActiveSheet.PivotTables("Tableau croisé dynamique2").PivotFields( _
"CREATION_DATE").CurrentPage = "A1"
But I am getting a debugging error.
After some testing, I found the cause (on my Excel Book). It was related to formatting the Pivot Field "CREATION_DATE", once I added the following section it worked.
Set PvtFld = PvtTbl.PivotFields("CREATION_DATE")
With PvtFld
.Orientation = xlPageField
.NumberFormat = "dd/mm/yyyy" ' << this line
.Position = 1
End With
And for the full code:
Sub PivotFilterDate()
Dim PvtTbl As PivotTable
Dim PvtFld As PivotField
' set your Pivot Table (after created) to a variable
Set PvtTbl = Sheets("PivotSpanish").PivotTables("Tableau croisé dynamique2")
' Set "CREATION_DATE" to a Pivot Field variable
Set PvtFld = PvtTbl.PivotFields("CREATION_DATE")
With PvtFld
.Orientation = xlPageField
.NumberFormat = "dd/mm/yyyy"
.Position = 1
End With
' you can modify your code to change all field setting, like following 2:
With PvtTbl.PivotFields("STATUS")
.Orientation = xlRowField
.Position = 1
End With
With PvtTbl.PivotFields("VENDOR_NAME")
.Orientation = xlRowField
.Position = 2
End With
' clear previous filters
PvtFld.ClearAllFilters
' --- trapping errors ---
' added a Boolean Flag to trap scenarios where no Pivot Item found that matches the value in Cell A1
Dim PvtItemExists As Boolean
PvtItemExists = False
' check all Pivot items in Pivot Field ("CREATION_DATE")
For Each PvtItem In PvtFld.PivotItems
' check if current Pivot Item equals the value in Cell A1
If PvtItem.Value = CStr(CDate(Range("D1").Value)) Then
PvtItemExists = True
Exit For
End If
Next PvtItem
If PvtItemExists Then
PvtFld.CurrentPage = CStr(CDate(Range("D1").Value))
Else
MsgBox "No data matches for date " & CDate(Range("D1").Value) & " in Pivot Table", vbCritical
End If
End Sub
I have the code below which theoretically should create a pivot table on a 2nd sheet (which exists) using the data it finds on the 'DATA' sheet. However it always crashes as soon as it reaches the part to create the pivot table.
I originally come from a fixed size and afterwards changed it so it should take all the data on my 'DATA' sheet regardless of whether it's a 2x3 or 58x13 table
Sheets("DATA").Select
Range("H1").Select
ActiveCell.FormulaR1C1 = "l"
Range("A1").Select
ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
Sheets("DATA").Range("A1").CurrentRegion, _
Version:=xlPivotTableVersion14).CreatePivotTable _
TableDestination:="Prior. per user!R1C1", TableName:="PivotTable1", _
DefaultVersion:=xlPivotTableVersion14
Sheets("Prior. per user").Select
Cells(1, 1).Select
With ActiveSheet.PivotTables("PivotTable1").PivotFields("Priority")
.Orientation = xlColumnField
.Position = 1
End With
With ActiveSheet.PivotTables("PivotTable1").PivotFields("Caller")
.Orientation = xlRowField
.Position = 1
End With
Range("D2").Select
ActiveSheet.PivotTables("PivotTable1").PivotFields("Priority").PivotItems( _
"Medium").Position = 2
Columns("D:D").ColumnWidth = 7.43
Columns("C:C").ColumnWidth = 10
ActiveSheet.PivotTables("PivotTable1").AddDataField ActiveSheet.PivotTables( _
"PivotTable1").PivotFields("Number"), "Sum of Number", xlSum
With ActiveSheet.PivotTables("PivotTable1").PivotFields("Sum of Number")
.Caption = "Count of Number"
.Function = xlCount
End With
If anyone sees what's wrong with it, it would be much appreciated.
Your target sheet name has spaces in it so you need to enclose it in single quotes:
ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
Sheets("DATA").Range("A1").CurrentRegion, _
Version:=xlPivotTableVersion14).CreatePivotTable _
TableDestination:="'Prior. per user'!R1C1", TableName:="PivotTable1", _
DefaultVersion:=xlPivotTableVersion14
Personally, I would also use a variable to refer to the pivot table:
Dim PT As PivotTable
Set PT = ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
Sheets("DATA").Range("A1").CurrentRegion, _
Version:=xlPivotTableVersion14).CreatePivotTable(TableDestination:="'Prior. per user'!R1C1", TableName:="PivotTable1", _
DefaultVersion:=xlPivotTableVersion14)
With PT
With .PivotFields("Priority")
.Orientation = xlColumnField
.Position = 1
End With
With .PivotFields("Caller")
.Orientation = xlRowField
.Position = 1
End With
.PivotFields("Priority").PivotItems("Medium").Position = 2
.Parent.Columns("D:D").ColumnWidth = 7.43
.Parent.Columns("C:C").ColumnWidth = 10
.AddDataField .PivotFields("Number"), "Count of Number", xlCount
End With
I am trying to record a macro which will create pivot chart out of excel data and here is the code that has been recorded:
Sub chart1()
'
' chart1 Macro
'
'
Range("E1:F11").Select
Sheets.Add
In Debugger, code within the **** **** is shown in Yellow color
***** ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
"data!R1C5:R11C6", Version:=xlPivotTableVersion12).CreatePivotTable _
TableDestination:="Sheet1!R3C1", TableName:="PivotTable1", DefaultVersion _
:=xlPivotTableVersion12 ********
Sheets("Sheet1").Select
Cells(3, 1).Select
With ActiveSheet.PivotTables("PivotTable1").PivotFields("question1")
.Orientation = xlRowField
.Position = 1
End With
With ActiveSheet.PivotTables("PivotTable1").PivotFields("answer1")
.Orientation = xlColumnField
.Position = 1
End With
ActiveSheet.PivotTables("PivotTable1").AddDataField ActiveSheet.PivotTables( _
"PivotTable1").PivotFields("answer1"), "Count of answer1", xlCount
With ActiveSheet.PivotTables("PivotTable1").PivotFields("answer1")
.Orientation = xlPageField
.Position = 1
End With
ActiveSheet.PivotTables("PivotTable1").PivotFields("answer1").Orientation = _
xlHidden
With ActiveSheet.PivotTables("PivotTable1").PivotFields("answer1")
.Orientation = xlColumnField
.Position = 1
End With
ActiveSheet.Shapes.AddChart.Select
ActiveChart.SetSourceData Source:=Range("Sheet1!$A$3:$D$6")
ActiveSheet.Shapes.AddChart.Select
ActiveChart.SetSourceData Source:=Range("Sheet1!$A$3:$D$6")
ActiveChart.ChartType = xlColumnClustered
End Sub
Why am I getting the Run Time Error 1004: Application-Defined or Object Defined Error when i try to run this macro ?
Thanks in advance..
Since you want the chart to be on a new sheet,
you have to change the macro's "Sheet1" to new worksheet's name,
The following macro should work for you, I have named the new worksheet as newWs
And fyi, your macro's error message I believe is due to trying to creating 2 pivot table of the same name on "Sheet1" is not allowed.
He also like to know what to create pivotTable base on Selected Area, so I have done modification to the code.
Edited: I Assume you select 2 columns each time
Sub chart1()
'
' chart1 Macro
'
'
Dim selectedSheetName As String
Dim newWs As Worksheet
Dim rangeName As String
Dim header1 As String
Dim header2 As String
header1 = ActiveSheet.Cells(1, Selection.Column).Value
header2 = ActiveSheet.Cells(1, Selection.Column + 1).Value
selectedSheetName = ActiveSheet.Name
rangeName = Selection.Address
Set newWs = Sheets.Add
ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
selectedSheetName & "!" & rangeName, Version:=xlPivotTableVersion12).CreatePivotTable _
TableDestination:=newWs.Name & "!R3C1", TableName:="PivotTable1", DefaultVersion _
:=xlPivotTableVersion12
newWs.Activate
Cells(3, 1).Select
With ActiveSheet.PivotTables("PivotTable1").PivotFields(header1)
.Orientation = xlRowField
.Position = 1
End With
With ActiveSheet.PivotTables("PivotTable1").PivotFields(header2)
.Orientation = xlColumnField
.Position = 1
End With
ActiveSheet.PivotTables("PivotTable1").AddDataField ActiveSheet.PivotTables( _
"PivotTable1").PivotFields(header2), "Count of answer1", xlCount
With ActiveSheet.PivotTables("PivotTable1").PivotFields(header2)
.Orientation = xlPageField
.Position = 1
End With
ActiveSheet.PivotTables("PivotTable1").PivotFields(header2).Orientation = _
xlHidden
With ActiveSheet.PivotTables("PivotTable1").PivotFields(header2)
.Orientation = xlColumnField
.Position = 1
End With
ActiveSheet.Shapes.AddChart.Select
ActiveChart.SetSourceData Source:=Range(newWs.Name & "!$A$3:$D$6")
ActiveSheet.Shapes.AddChart.Select
ActiveChart.SetSourceData Source:=Range(newWs.Name & "!$A$3:$D$6")
ActiveChart.ChartType = xlColumnClustered
End Sub