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

时间:2017-11-09 06:08:53

标签: vba excel-vba excel

我的代码有什么问题?

我收到了错误

  

方法激活工作表失败

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

1 个答案:

答案 0 :(得分:1)

Activate ws无需过滤PivotTable

尝试下面的代码,代码注释中的解释:

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