我正在尝试查看用户表单中的复选框,并根据用户选择将其显示在数据透视表中
我使用以下代码:
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中时,它工作正常但是如果我试图将它放在不同的模块中,我会得到“Sub或function not defined”错误,并且代码中会突出显示“Controls”。 有谁知道我做错了什么?
答案 0 :(得分:1)
这是你想要完成的另一种方式(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