VBA多图表过滤器

时间:2017-10-17 16:49:20

标签: vba excel-vba excel

VBA很新。我有一个工作簿,在一个选项卡上有大约8个图表,并且有很多选项卡。每个图表都需要针对相同的值进行过滤,我正在努力让我的宏工作。这就是我所拥有的:

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

你知道我哪里出错吗?

谢谢!

1 个答案:

答案 0 :(得分:2)

代码中有许多地方语法错误。我在下面重新编写并测试过。假设您的图表为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