Excel VBA错误438在combobox表单validation
我想validation我的多页控件的第2页。 它有2个combobox,2个单选button和几个文本框。
基本上,所有的领域都必须具有价值。
这是我正在使用的代码:
Private Sub validaPasso3() Dim cCont As Control For Each cCont In Me.MultiPage1.Pages(2).Controls If cCont.Value = vbNullString Then MsgBox "Error." Exit Sub End If Next End Sub
但是这是返回错误:
Run-time error: ´438´ Object doesn't support this property or method.
在线:
If cCont.Value = vbNullString Then
我知道这是由第一个combobox造成的。
什么是导致错误?
如果在combobox中没有选中,它将返回Null
而不是vbNullString
。 你可以用这样的东西代替:
Private Sub validaPasso3() Dim cCont As Control For Each cCont In Me.MultiPage1.Pages(2).Controls If Typename(cCont) = "ComboBox" Then If IsNull(cCont.Value) Then MsgBox "Error." Exit Sub End If ElseIf Typename(cCont) = "TextBox" Then If cCont.Value = vbNullString Then Msgbox "Error." Exit Sub End If ElseIf Typename(cCont) = "OptionButton" Then If cCont.Value = False Then MsgBox "Error." Exit Sub End If End If Next End Sub