1

Pretty new to VBA. I have a workbook that has about 8 charts on a tab and there are a lot of tabs. Each chart needs to be filtered for the same values, and I'm struggling to get my macro to work. Here's what I have:

Sub ChartFilter()
'
' 
'

'
For Each Chart In ActiveWorkbook.Charts
    ActiveChart.Legend.Select
    With ActiveChart.PivotLayout.PivotTable.PivotFields("Category1")
        .PivotItems("Value1").Visible = True
        .PivotItems("Value2").Visible = True
        .PivotItems("Value3").Visible = True
        .PivotItems("Value4").Visible = True
        .PivotItems("Value5").Visible = True
        .PivotItems("Value6").Visible = True
        .PivotItems("Value7").Visible = True
    End With
    Next Chart
End Sub

Do you know where I'm going wrong?

Thanks!

1
  • are the charts pivotcharts? Commented Oct 17, 2017 at 16:53

1 Answer 1

2

There are many places in the code where the syntax is wrong. I have re-written and tested below. This assumes your charts are PivotCharts

Dim ws As Worksheet

For Each ws In ThisWorkbook.Worksheets

    Dim pt As PivotTable

    For Each pt In ws.PivotTables

        Dim pi As PivotItem

        For Each pi In pt.PivotFields("Category1").PivotItems

            Select Case pi.Name

                Case Is = "Value1", "Value2", "Value3", "Value4", "Value5", "Value6", "Value7"
                    pi.Visible = True
                Case Else
                    pi.Visible = False

            End Select

        Next

    Next

Next
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, Scott! Definitely a lot left to learn, but I appreciate you helping me out with this project.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.