VBA多个复选框可控制多个数据透视表

时间:2015-04-29 12:48:43

标签: vba checkbox pivot-table

我再次需要一些帮助,我将非常感激。

基本上,在我的仪表板页面上,我有几个复选框可以在后台控制多个数据透视表。

我的复选框名为“明确”,“暂定”,“待定”,......也对应于透视字段中的值。

我在不同的工作表中有许多名为“Hidden_​​1”或“Hidden_​​2”的数据透视表,但都具有相同的结构。

我的想法是,如果有人选中“确定”,则会在名为“状态”的字段中的所有数据透视表中选择它。如果有人“取消选中”此复选框,则枢轴将做出反应。

为此,我使用了之前创建的代码并且运行良好:

    Sub checkbox1()
Application.ScreenUpdating = False
On Error Resume Next  
Dim pt As PivotTable, wks As Worksheet
For Each wks In ActiveWorkbook.Worksheets
    For Each pt In wks.PivotTables
        With pt
            If .Name = "Hidden_1" Or .Name = "Hidden_2" Then
                .PivotFields("Status").CurrentPage = "definite"
            End If
        End With
    Next pt
Next wks 
Application.ScreenUpdating = True 
End Sub

但是,此代码只选择一个值,因此如果有人选中了这些框,我就无法选择“明确”和“待定”。现在所有的复选框都有一个单独的代码,只有.CurrentPage =“checkboxname”被更改了..

我有两个问题:

1)选择多个值的最佳方法是什么。例如。如果选中复选框“明确”和“待定”,则数据透视表应选择在“状态”字段中选择两个“明确”和“待定”值

2)“取消选择”价值的最佳方式是什么?现在,每次单击复选框时,我的程序checkbox1都会运行。而且我希望它只在我“检查”时运行。 现在我正在尝试将复选框与单元格链接,例如“明确”有H10,所以我的代码以行开头:

If Range("H10").Value = True Then
'code to select the value in "Status" field
Else
'code to unselect the value in "Status" field
End If

我还应该注意到我无法使用ActiveX Checkbox因为我有错误:“无法插入对象”而我使用了表单控件。我读到这个错误与我安装的补丁有某种联系。

谢谢大家的帮助, 马特

1 个答案:

答案 0 :(得分:1)

我参与其中并找到了这样的解决方案:

Sub checkbox1()
Dim choice1, choice2, choice3, choice4, choice5, choice6, choice7
Dim oPI As PivotItem
Dim pt As PivotTable, wks As Worksheet

If Sheets("Hidden").Range("B6").Value = "True" Then
    choice1 = "Definite"
End If
If Sheets("Hidden").Range("B7").Value = "True" Then
    choice2 = "Tentative"
End If
If Sheets("Hidden").Range("B8").Value = "True" Then
    choice3 = "Hold/Option"
End If
If Sheets("Hidden").Range("B9").Value = "True" Then
    choice4 = "Pending"
End If
If Sheets("Hidden").Range("B10").Value = "True" Then
    choice5 = "Waitlist"
If Sheets("Hidden").Range("B11").Value = "True" Then
    choice6 = "Lost"
End If
If Sheets("Hidden").Range("B12").Value = "True" Then
    choice7 = "Cancelled"
End If
Sheets("Hidden_pivot1").PivotTables("Hidden_1").PivotFields("SalesStatus").ClearAllFilters
Sheets("Hidden_pivot1").PivotTables("Hidden_3").PivotFields("SalesStatus").ClearAllFilters
Sheets("Hidden_pivot2").PivotTables("Hidden_2").PivotFields("SalesStatus").ClearAllFilters
Sheets("Hidden_pivot2").PivotTables("Hidden_4").PivotFields("SalesStatus").ClearAllFilters

For Each wks In ActiveWorkbook.Worksheets
    For Each pt In wks.PivotTables
             With pt
                If .Name = "Hidden_1" Or .Name = "Hidden_2" Or .Name = "Hidden_3" Or .Name = "Hidden_4" Then
                    For Each oPI In pt.PivotFields("SalesStatus").PivotItems
                        Select Case oPI.Name
                                 Case choice1, choice2, choice3, choice4, choice5, choice6, choice7
                                 Case Else
                                oPI.Visible = False
                        End Select
                    Next
                End If
            End With
     Next pt
Next wks
End Sub

这项工作但速度很慢。如果宏可以添加和删除这些项目,而不是重新创建整个选项,那会更好。