Hi there,
I hope someone might be able to help me with this, I have a macro that updates a table from a SQL Server view, refreshes a pivot table and moves the axis' along for a gantt chart according to the date. My problem is I need to pivot table to show all items except where Value of Free =0 but I get an error when running this code
"Runtime-Error 1004 Unable to set the visible property of the PivotItem class"
stops at "Pi.Visible = True"
I hope someone might be able to help me with this, I have a macro that updates a table from a SQL Server view, refreshes a pivot table and moves the axis' along for a gantt chart according to the date. My problem is I need to pivot table to show all items except where Value of Free =0 but I get an error when running this code
"Runtime-Error 1004 Unable to set the visible property of the PivotItem class"
stops at "Pi.Visible = True"
Code:
Sub Macro1()
Application.DisplayAlerts = False
Dim mydate As Date
Dim AxisStart As Date
Dim AxisEnd As Date
Dim pt As PivotTable
Dim Pi As PivotItem
mydate = Date + 28
AxisStart = Date - 140
AxisEnd = Date + 112
Sheets("Data").Select
Range("A1").Select
Selection.ListObject.QueryTable.Refresh BackgroundQuery:=False
Sheets("Pivot").Select
Range("A7").Select
ActiveSheet.PivotTables("PivotTable7").PivotCache.Refresh
ActiveSheet.PivotTables("PivotTable7").PivotFields("No_").ClearAllFilters
ActiveSheet.PivotTables("PivotTable7").PivotFields("No_").PivotFilters.Add _
Type:=xlValueIsLessThan, DataField:=ActiveSheet.PivotTables("PivotTable7"). _
PivotFields("Max of Order By"), Value1:=mydate
Set pt = ActiveSheet.PivotTables("PivotTable7")
For Each Pi In pt.PivotFields("Free").PivotItems
Pi.Visible = True
Next Pi
With ActiveSheet.PivotTables("PivotTable7").PivotFields("Free")
.PivotItems("0").Visible = False
End With
Sheets("Chart2").Select
' ActiveSheet.ChartObjects("Chart 2").Activate
ActiveChart.Axes(xlValue, xlSecondary).MinimumScale = AxisStart
ActiveChart.Axes(xlValue, xlSecondary).MaximumScale = AxisEnd
ActiveChart.Axes(xlValue).MinimumScale = AxisStart
ActiveChart.Axes(xlValue).MaximumScale = AxisEnd
Application.DisplayAlerts = True
End Sub