1

我正在尝试查看用户表单中的复选框并根据用户选择将它们显示在数据透视表中

我使用以下代码:

Dim mthIdx As Long
Dim nm As String
Dim c As Control
With ActiveSheet.PivotTables(CakePivot2).PivotFields("month")
    For mthIdx = 1 To 12
        nm = "CheckBox" & mthIdx
        Set c = Controls(nm)
        .PivotItems(mthIdx).Visible = printing_rep.c.Value
    Next
End With

当我将它放入用户表单 privete sub 时它工作正常,但如果我试图将它放入不同的模块中,我会收到“未定义子或函数”错误,并且代码中突出显示“控件”。有谁知道我做错了什么?

4

1 回答 1

1

这是您尝试完成的另一种方法(未测试)

Sub Sample()
    Dim mthIdx As Long
    Dim c As Control
    Dim ID As String

    mthIdx = 0

    With ActiveSheet.PivotTables(PivotTable1).PivotFields("month")
        For Each c In printing_rep.Controls
            If TypeOf c Is MSForms.CheckBox Then
                ID = Replace(c.Name, "CheckBox", "")

                Select Case Val(Trim(ID))
                Case 1 To 12: mthIdx = Val(Trim(ID))
                End Select

                If mthIdx <> 0 Then
                    .PivotItems(mthIdx).Visible = c.Value
                    mthIdx = 0
                End If
            End If
    End With
End Sub
于 2013-09-12T08:35:02.507 回答