在VBA Excel中作为控件昏暗

我试图通过用户窗体中的checkbox,并根据用户select显示在数据透视表中

我使用下面的代码:

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 

它工作正常,当我把它放在一个用户formsprivete子,但如果我想把它放在一个不同的模块中,我得到“子或function未定义”的错误和“控制”在代码中突出显示。 有谁知道我做错了什么?

这是你试图完成的另一种方法(UNTESTED)

 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