I am trying to generate 2 charts based on a pivot table with data pulled from a microsoft access table with conditions and filters based on button click, however everytime i tried to run a macro for a button script for the 2nd button i get the error on the title.
This is the code that is not functioning
Private Sub CommandButton2_Click()
Dim sh As Shape
Dim ch As Chart
Dim pc As PivotCache
Dim ws As Worksheet
Dim pt As PivotTable
Dim NameString As String
Dim Pf As PivotField
Dim NumFilter As Integer
NameString = "Department Comparison"& ""& TextBox1.Value
Set pc = ThisWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:=Sheet1.Name & "!"& Sheet1.Range("A1").CurrentRegion.Address, Version:=xlPivotTableVersion15)
Set ws = Worksheets.Add
ws.Name = NameString
Range("A3").Select
Set pt = pc.CreatePivotTable(TableDestination:=ActiveCell, TableName:=NameString)
pt.AddFields RowFields:="Ms", PageFields:="Y", ColumnFields:="D"
pt.AddDataField pt.PivotFields("RT"), Function:=XlConsolidationFunction.xlSum
pt.AddDataField pt.PivotFields("ORT"), Function:=XlConsolidationFunction.xlSum
pt.AddDataField pt.PivotFields("EA"), Function:=XlConsolidationFunction.xlSum
pt.AddDataField pt.PivotFields("PA"), Function:=XlConsolidationFunction.xlSum
pt.DataFields(1).NumberFormat = "#,##0"
Set ws = Worksheets(NameString)
Set sh = ws.Shapes.AddChart2(XlChartType:=XlChartType.xlLine, Left:=300, Top:=500, Width:=500, Height:=500)
Set ch = sh.Chart
Set pt = ws.PivotTables(1)
ch.SetSourceData pt.TableRange2
Set Pf = ActiveSheet.PivotTables(NameString).PivotFields("Y")
Pf.CurrentPage = TextBox1.Value
pt.PivotFields("Ms").AutoSort Order:=xlAscending, Field:="Sum of ORT"
Set pc = ThisWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:=Sheet2.Name & "!"& Sheet2.Range("A1").CurrentRegion.Address, Version:=xlPivotTableVersion15)
Range("J3").Select
Set pt = pc.CreatePivotTable(TableDestination:=ws.Cells(3, 11), TableName:=NameString & " Employees")
pt.AddFields RowFields:="Month", PageFields:="Year", ColumnFields:="Department"
pt.AddDataField pt.PivotFields("RT"), Function:=XlConsolidationFunction.xlSum
pt.DataFields(1).NumberFormat = "#,##0"
Set ws = Worksheets(NameString)
Set sh = ws.Shapes.AddChart2(XlChartType:=XlChartType.xlLine, Left:=800, Top:=500, Width:=500, Height:=500)
Set ch = sh.Chart
Set pt = ws.PivotTables(NameString & " Employees")
ch.SetSourceData pt.TableRange2 ///This is where the debugger points at the error
Set Pf = ActiveSheet.PivotTables(NameString & " Employees").PivotFields("Year")
Pf.CurrentPage = TextBox1.Value
End Sub
I had the first button with almost the same code with the exceptionfor setting the current page fields and it worked fine but this is the one that had issues,i compared both side to side and they are 99% the same
First button click code:
Private Sub CommandButton1_Click()
Dim sh As Shape
Dim ch As Chart
Dim pc As PivotCache
Dim ws As Worksheet
Dim pt As PivotTable
Dim NameString As String
Dim Pf As PivotField
Dim NumFilter As Integer
NameString = ComboBox2.Value & ""& TextBox1.Value
Set pc = ThisWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:=Sheet1.Name & "!"& Sheet1.Range("A1").CurrentRegion.Address, Version:=xlPivotTableVersion15)
Set ws = Worksheets.Add
ws.Name = NameString
Range("A3").Select
Set pt = pc.CreatePivotTable(TableDestination:=ActiveCell, TableName:=NameString)
pt.AddFields RowFields:="Ms", PageFields:=Array("D", "Y")
pt.AddDataField pt.PivotFields("RT"), Function:=XlConsolidationFunction.xlSum
pt.AddDataField pt.PivotFields("ORT"), Function:=XlConsolidationFunction.xlSum
pt.AddDataField pt.PivotFields("EA"), Function:=XlConsolidationFunction.xlSum
pt.AddDataField pt.PivotFields("PA"), Function:=XlConsolidationFunction.xlSum
pt.DataFields(1).NumberFormat = "#,##0"
Set ws = Worksheets(NameString)
Set sh = ws.Shapes.AddChart2(XlChartType:=XlChartType.xlLine, Left:=300, Top:=500, Width:=500, Height:=500)
Set ch = sh.Chart
Set pt = ws.PivotTables(1)
ch.SetSourceData pt.TableRange2
Set Pf = ActiveSheet.PivotTables(NameString).PivotFields("Y")
Pf.CurrentPage = TextBox1.Value
Set Pf = ActiveSheet.PivotTables(NameString).PivotFields("D")
Pf.CurrentPage = ComboBox2.Value
pt.PivotFields("Ms").AutoSort Order:=xlAscending, Field:="Sum of ORT"
NameString = ComboBox2.Value & ""& TextBox1.Value
Set pc = ThisWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:=Sheet2.Name & "!"& Sheet2.Range("A1").CurrentRegion.Address, Version:=xlPivotTableVersion15)
Range("J3").Select
Set pt = pc.CreatePivotTable(TableDestination:=ws.Cells(3, 11), TableName:=NameString & " Employees")
pt.AddFields RowFields:="Month", PageFields:="Year", ColumnFields:="Department"
pt.AddDataField pt.PivotFields("RT"), Function:=XlConsolidationFunction.xlSum
pt.DataFields(1).NumberFormat = "#,##0"
Set ws = Worksheets(NameString)
Set sh = ws.Shapes.AddChart2(XlChartType:=XlChartType.xlLine, Left:=800, Top:=500, Width:=500, Height:=500)
Set ch = sh.Chart
Set pt = ws.PivotTables(NameString & " Employees")
ch.SetSourceData pt.TableRange2
Set Pf = ActiveSheet.PivotTables(NameString & " Employees").PivotFields("Year")
Pf.CurrentPage = TextBox1.Value
Set Pf = ActiveSheet.PivotTables(NameString & " Employees").PivotFields("Department")
With ActiveSheet.PivotTables(NameString & " Employees").PivotFields("Department")
For i = 1 To .PivotItems.Count - 1
.PivotItems(.PivotItems(i).Name).Visible = False
Next i
End With
Pf.PivotItems(ComboBox2.Value).Visible = True
End Sub