使用通配符*

时间:2017-08-30 14:08:22

标签: excel-vba vba excel

以下代码取消选择以" xyznames"开头的PivotItems。使用通配符" *。"但它的工作原理非常慢。任何想法做同样的事情,但更快?

Sub UncheckxyzName()
'Purpose: Uncheck "xyzname* from Pivot tables from "Comparison" tab

 Dim pt As PivotTable
 Dim myPI As PivotItem

'stop Excel actions for speed enhancement
With Excel.Application
   .ScreenUpdating = False
   .EnableEvents = False
   .Calculation = Excel.xlCalculationManual
   .DisplayStatusBar = False
End With

'uncheck "xyzname*" from "Comparison" tab
With Worksheets("Comparison").PivotTables("PivotTable1").PivotFields("Type")
    .PivotItems("fnBid").Visible = False
End With

 With Worksheets("Comparison").PivotTables("PivotTable1").PivotFields("Resource Name")

 For Each myPI In .PivotItems
 myPI.Visible = True
 Next myPI

 For Each myPI In .PivotItems
    If myPI.name Like "xyzname*" Then
        myPI.Visible = False
    Else
        myPI.Visible = True
    End If
    Next myPI
 End With

'Turn back on Excel actions 
With Excel.Application
    .DisplayStatusBar = True
    .Calculation = Excel.xlCalculationAutomatic
    .EnableEvents = True
    .ScreenUpdating = True
End With

End Sub

与帖子相关:

excel macro vba Filtering using wildcards

1 个答案:

答案 0 :(得分:1)

这个循环:

For Each myPI In .PivotItems
myPI.Visible = True
Next myPI

是不必要的,您可以遍历每个项目两次,一次使它们全部可见,然后根据Like条件显示或不显示。假设你的宏缓慢的原因是迭代大量的PivotItems删除这个循环会使它快两倍。