使用“if”循环在运行时dynamic检查checkbox的值并执行操作

我有一个Excel VBA代码。 我将有一个用户表单,并在运行时从电子表格中获取数据,用ckeckbox填充用户表单。 现在,当我点击确定buton我需要代码来检查checkbox的值。 如果任何checkbox被选中,则相应地应该执行一些操作。 我已经使用“IF”循环。 但是代码不会进入IF循环。 它只是跳过它。 我有下面的示例代码。

Private Sub CommandButton1_Click() lastrow = Range("D65536").End(xlUp).Row For idx = 1 To latrow If Me.Controls("cname" & idx).Value = True Then 'my code End If Next idx UserForm1.Hide End Sub Private Sub selectall() Dim idx As Integer lastrow = Range("D65536").End(xlUp).Row - 4 Dim chk As Control For idx = 1 To lastrow Me.Controls("cname" & idx).Value = True Next idx End Sub Private Sub userform_initialize() Dim idx As Integer lastrow = Range("D65536").End(xlUp).Row - 4 Dim EachLabel As Integer Dim cCntrl As Control Dim chk As Control Dim tbindex As Integer For idx = 1 To lastrow Set chk1 = Frame1.Controls.Add("Forms.checkbox.1", "cname" & idx, True) chk1.Caption = Cells(4 + idx, 4).Value chk1.Visible = True chk1.Left = 6 chk1.Top = 1 + idx * 17.5 chk1.Width = 100 chk1.Height = 15.75 Next UserForm1.Frame1.ScrollBars = fmScrollBarsVertical UserForm1.Frame1.ScrollHeight = 20 * lastrow End Sub