简体   繁体   English

VBA-工作表的方法激活失败?

[英]VBA - Method Activate of Worksheet Failed?

What is wrong with my code below? 我下面的代码有什么问题?

I got an error 我有一个错误

Method Activate of Worksheet Failed 工作表的方法激活失败

Sub addMOnth()

Dim ws As Worksheet

For Each ws In ActiveWorkbook.Worksheets
    ws.Activate    
    If ws.Name <> "SUMMARY" And ws.Name <> "Store" And ws.Name <> "Apps" Then

        ActiveSheet.PivotTables("PivotTable1").PivotFields( _
        "[Report Date Structure].[Month].[Month]").VisibleItemsList = Array( _
        "[Report Date Structure].[Month].&[2.01711E5]")        
    End If
Next ws

End Sub

There is no need to Activate the ws just to filter the PivotTable . 无需Activate ws仅过滤PivotTable

Try the code below, explanation inside the code's comments: 尝试以下代码,并在代码注释中进行解释:

Sub addMOnth()

Dim ws As Worksheet

For Each ws In ThisWorkbook.Worksheets
    With ws
        Select Case .Name
            Case "SUMMARY", "Store", "Apps"
                ' do nothing

            Case Else ' equivalent to your If with multiple AND
                .PivotTables("PivotTable1").PivotFields( _
                    "[Report Date Structure].[Month].[Month]").VisibleItemsList = Array( _
                    "[Report Date Structure].[Month].&[2.01711E5]")
        End Select            
    End With
Next ws

End Sub

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM