这一行发生了什么?
What's happening in this line?
我创建了一个名为 "questionario" 的带有复选框的表单,并制作了一个 IF 语句以在所有复选框为空时发送一个消息框。但是当我 运行 宏时,引用行中出现错误(错误 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") And (cont.Value = True) Then
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
原因是有些控件类型没有.Value
属性,VBA没有短路布尔表达式。因此,即使 cont.TypeName
<> "CheckBox",表达式仍会尝试查询那些可能没有 属性 的控件的 .Value
属性。
我创建了一个名为 "questionario" 的带有复选框的表单,并制作了一个 IF 语句以在所有复选框为空时发送一个消息框。但是当我 运行 宏时,引用行中出现错误(错误 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") And (cont.Value = True) Then
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
原因是有些控件类型没有.Value
属性,VBA没有短路布尔表达式。因此,即使 cont.TypeName
<> "CheckBox",表达式仍会尝试查询那些可能没有 属性 的控件的 .Value
属性。