如何使用切片器加速这个VBA代码?

时间:2017-02-16 20:57:41

标签: excel vba excel-vba slicers

我有一个包含七个表(tbl_1,tbl_2 ... tbl_7)的电子表格,每个表都由自己的切片器控制。每个切片器有六个按钮(10,20,30,40,50,60),参考团队代码。我使用下面的代码在每个切片器上选择一个团队,然后为每个团队/切片器设置创建一个PDF。截至目前,代码需要5-7分钟才能运行。任何帮助深表感谢。

Sub SlicerTeam()
Dim wb As Workbook
Dim sc As SlicerCache
Dim si As SlicerItem

On Error GoTo errHandler
Application.ScreenUpdating = False
Application.EnableEvents = False

Set wb = ThisWorkbook

For x = 1 To 6
    For i = 1 To 7
    Set sc = wb.SlicerCaches("tbl_" & i)
        sc.ClearAllFilters
        For Each si In sc.VisibleSlicerItems
            Set si = sc.SlicerItems(si.Name)
                If Not si Is Nothing Then
                    If si.Name = x * 10 Then
                        si.Selected = True
                    Else
                        si.Selected = False
                    End If
                Else
                    si.Selected = False
                End If
        Next si

    Next i
Call PDFCreate
Next x

exitHandler:
Application.ScreenUpdating = True
Application.EnableEvents = True
Exit Sub

errHandler:
MsgBox ("Error in updating slicer filters.")
Resume exitHandler

End Sub

2 个答案:

答案 0 :(得分:1)

假设这些切片器正在切换数据透视表,请尝试以下代码。它可能有助于加快速度,具体取决于您的数据透视表的大小。

----------------------------SLA----------------------------
 ,CAST ((sum(case WHEN datediff(minute,(select max(ENTRY_DATE) from history_entry where a.job_Ticket_ID = HISTORY_ENTRY.job_Ticket_ID and HISTORY_ENTRY.ENTRY_TEXT like ('%Escalated to %%NOC%Level 2%%') and HISTORY_ENTRY.ENTRY_TEXT not like ('%Escalated to %%NOC%Level 1%')),
 (select max(ENTRY_DATE) from history_entry where a.job_Ticket_ID = HISTORY_ENTRY.job_Ticket_ID and HISTORY_ENTRY.ENTRY_TEXT like ('%Assigned to %%NOC%Level 2%') and HISTORY_ENTRY.ENTRY_TEXT not like ('%Assigned to %%NOC%Level 1%'))) <= 10         and 
 datediff(MINUTE,(select max(ENTRY_DATE) from history_entry where a.job_Ticket_ID = HISTORY_ENTRY.job_Ticket_ID and HISTORY_ENTRY.ENTRY_TEXT like ('%Escalated to %%NOC%Level 2%%') and HISTORY_ENTRY.ENTRY_TEXT not like ('%Escalated to %%NOC%Level 1%')),
 (select max(ENTRY_DATE) from history_entry where a.job_Ticket_ID = HISTORY_ENTRY.job_Ticket_ID and HISTORY_ENTRY.entry_text like '%Status changed from % to closed%%')) <= case PRIORITY_TYPE_NAME
                                   WHEN 'low' then 960
                                   WHEN 'medium' then 480
                                   WHEN 'high' then 120
                                   WHEN 'Urgent' then 60
                                   end
  then 1
  else 0
  end)*100.0)/COUNT(*) as money) as Percent_Compliant

答案 1 :(得分:0)

经过几次试验..发现这是最好的选择。

  1. 禁用计算:

     @Entity(tableName = "users")
     public class UserEntity {
        @ColumnInfo(name = "id")
        @PrimaryKey(autoGenerate = true)
        private int id;
        @ColumnInfo(name = "username")
        private String username;
        @ColumnInfo(name = "password")
        private String password;
        @ColumnInfo(name = "email")
        private String email;
    
  2. 类型代码以删除切片器的锥角...。示例:

    Application.ScreenUpdating = False
    With Application
    .EnableEvents = False
    .Calculation = xlCalculationManual
    End With
    
  3. 将slicer值设置为true,其他设置为false ...例如:

    ActiveWorkbook.SlicerCaches("Slicer_Area").PivotTables.RemovePivotTable ( _
        ActiveSheet.PivotTables("PivotDatosGraficoAbsoluto"))
    
  4. 执行切片器连接..例如:

    Set MySlicerCache = ActiveWorkbook.SlicerCaches("Slicer_Area")
                For i = 1 To MySlicerCache.SlicerItems.Count
                    With MySlicerCache.SlicerItems(i)
                        If .Name = "Comercial GJ" Then
                            .Selected = True
                            'Range("E1").Value = .Name
                        Else:
                            .Selected = False
                        End If
                    End With
                Next i
    
  5. 启用事件:

    ActiveWorkbook.SlicerCaches("Slicer_Area").PivotTables.AddPivotTable ( _
         ActiveSheet.PivotTables("PivotDatosGraficoAbsoluto"))
    

    结尾为

这将节省大约40%的等待时间