2017-10-17 86 views
1

VBA很新。我有一個工作簿,在選項卡上有大約8個圖表,並且有很多選項卡。每個圖表都需要根據相同的值進行過濾,並且我正在努力讓我的宏工作。下面是我有:VBA多圖表過濾器

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 

你知道我要去哪裏錯了嗎?

謝謝!

+0

是圖表數據透視圖? –

回答

1

代碼中有許多地方的語法錯誤。我已在下面重新編寫和測試。這裏假設你的圖表是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 
+0

謝謝,斯科特!肯定還有很多東西需要學習,但是我很感謝你幫助我完成這個項目。 –