这一行发生了什么?

我用checkbox创build了一个名为“questionario”的表单,如果所有的chebox都是空白的,就发一个IF语句发送一个msgbox。 但是,当我运行macros引用行中出现错误(错误438)。

Private Sub CommandButton1_Click() Dim ind As Integer Dim cont As MSForms.Control ind = 0 If questionario.resp1.Value = True Then Range("E8").Value = Range("E8").Value + 1 End If If questionario.resp2.Value = True Then Range("F8").Value = Range("F8").Value + 1 End If If questionario.resp3.Value = True Then Range("G8").Value = Range("G8").Value + 1 End If For Each cont In questionario.Controls 

如果(TypeName(cont)=“CheckBox”)和(cont.Value = True)那么

 ind = ind + 1 End If Next If ind = 0 Then MsgBox "mmm" Else questionario.Hide Set questionario = Nothing End If End Sub 

把你的支票分成两步:

 For Each cont In questionario.Controls If TypeName(cont) = "CheckBox" Then If cont.Value Then '<-- a checkbox control has a Value property ind = ind + 1 Exit For '<-- no need to go on End If End If Next 

原因是一些控件types没有.Value属性,VBA不会短路布尔expression式。 因此,即使cont.TypeName <>“CheckBox”,该expression式仍然试图查询那些可能没有这种操作的控件的.Value属性。