说明
我没有让我的 Forms.ComboBox 在Change
事件上更改。事件代码似乎没有触发。但它确实如此!但是,如果我逐步完成代码,它可以完美运行。不知道为什么会这样。
应该发生什么:
- "CauseBox1"更改(用户从其下拉列表中选择(
- 如果 CauseBox1 中的值包含"原因"一词,则应启用"注释框 1"。如果不是,则应禁用"CommentaryBox1"。
- 我正在使用一个名为
Editing
的布尔值,我将其设置为在代码开头True
(并在代码末尾False
(。原因是我正在使用MsForms Combobox。他们的Change
事件代码即使Application.EnableEvents = False
.因此,这是一种预防措施,以确保在此事件运行时没有其他Change
事件运行......
Private Sub CauseBox1_Change()
If Editing Then Exit Sub
Editing = True
If InStr(1, CauseBox1.Text, "reason") > 0 Then
If CommentaryBox1.Enabled = False Then CommentaryBox1.Enabled = True
Else
If CommentaryBox1.Enabled = True Then CommentaryBox1.Enabled = False
End If
Editing = False
End Sub
这是它出错的地方:
-
CauseBox1
变化,"原因"在其值中找到 -
CommentaryBox1
保持禁用状态。我不能输入它。 - 为了使
CommentaryBox1
启用,我必须使用F8
单步执行Change
事件代码。然后它突然起作用了。
为什么会这样?我错过了什么?
编辑:我在工作表中,而不是用户窗体中。
我有一个按钮可以清除工作表中的所有OLEObjects(组合框/文本框(。那段代码把事情搞砸了。最后一段代码,oCommentary1.Object.Enabled = False
,是造成错误的原因。由于我使用Change
事件,因此不需要强制禁用Commentary
框。每当我更改值(如上面的代码中所示(时,事件都会触发。我真的不能说是什么导致了这个错误。将MsForms.ComboBox(ActiveX(称为OLEObject
是否正确?
Public oDate As OLEObject, oAge As OLEObject, oGender As OLEObject, oInitials As OLEObject
Public oCause1 As OLEObject, oCause2 As OLEObject, oCause3 As OLEObject
Public oCommentary1 As OLEObject, oCommentary2 As OLEObject, oCommentary3 As OLEObject
Sub ClearForm()
With Worksheets("Form")
Set oDate = .OLEObjects("DateBox")
Set oInitials = .OLEObjects("InitialBox")
Set oGender = .OLEObjects("GenderBox")
Set oAge = .OLEObjects("AgeBox")
Set oCause1 = .OLEObjects("CauseBox1")
Set oCause2 = .OLEObjects("CauseBox2")
Set oCause3 = .OLEObjects("CauseBox3")
Set oCommentary1 = .OLEObjects("CommentaryBox1")
Set oCommentary2 = .OLEObjects("CommentaryBox2")
Set oCommentary3 = .OLEObjects("CommentaryBox3")
End With
oDate.Object.Value = ""
oInitials.Object.Value = ""
oGender.Object.Value = ""
oAge.Object.Value = ""
oCause1.Object.Value = ""
oCommentary1.Object.Value = ""
oCause2.Object.Value = ""
oCommentary2.Object.Value = ""
oCause3.Object.Value = ""
oCommentary3.Object.Value = ""
oDate.Activate ' Selects the date box
' Disables the Commentary Boxes (since the Cause Boxes are empty)
oCommentary1.Object.Enabled = False
oCommentary2.Object.Enabled = False
oCommentary3.Object.Enabled = False
End Sub