[英]Dim as Control in VBA Excel
我正在嘗試瀏覽用戶表單中的復選框,並根據用戶選擇將其顯示在數據透視表中
我使用以下代碼:
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”。 有人知道我在做什么錯嗎?
這是您嘗試完成的另一種方法(未測試)
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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.