这一行发生了什么?

时间:2016-10-24 15:57:14

标签: excel vba userform

我创建了一个名为" questionario"使用复选框,如果所有chebox都为空,则发出一个IF语句来发送msgbox。但是当我运行宏时,引用行中会出现错误(错误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
  

If(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

1 个答案:

答案 0 :(得分:2)

将支票分成两个步骤:

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

原因是某些控件类型没有.Value属性,并且VBA不会使布尔表达式短路。因此,即使cont.TypeName&lt;&gt; &#34; CheckBox&#34;,表达式仍然试图查询那些可能没有这种属性的控件的.Value属性。

相关问题