为什么我的复选框在我尝试使用 sheet 上的按钮启动我的用户窗体时不起作用,而当我从主菜单启动用户窗体时却起作用?
Why my checkboxes doesn't work when I try to launch my userform with a button on the sheet and work when I launch the userform from the main menu?
我目前正在 VBA 中进行一个小项目,因此我创建了一个带有两个复选框的用户窗体,并为这些复选框编写了 2 个子项,以确保只有一个复选框可以检查。这是 2 个复选框的代码:
Private Sub CheckBox2_Click()
If CheckBox2.Value = True And CheckBox3.Value = True Then
CheckBox3.Value = False
TextBox8.Enabled = True
TextBox8.BackColor = RGB(255, 255, 255)
Else
CheckBox3.Value = False
CheckBox2.Value = True
End If
End Sub
Private Sub CheckBox3_Click()
If CheckBox3.Value = True And CheckBox2.Value = True Then
CheckBox2.Value = False
TextBox8.Enabled = False
TextBox8.BackColor = RGB(205, 205, 205)
Else
CheckBox2.Value = False
CheckBox3.Value = True
TextBox8.Enabled = False
End If
End Sub
如您所见,subs 还 enable/disables 考虑到选中复选框的 TextBox 字段。
当我尝试使用 运行 按钮从主菜单启动用户窗体时,此代码完美运行:如果您选中一个复选框,另一个将被取消选中。
我还在我的 Excel 文件的第一个 sheet 上创建了一个启动用户窗体的按钮,这样用户就不必激活开发人员菜单来启动它。这是附加到此按钮的子项:
Private Sub lance_interface()
UF.Show
End Sub
当我单击此按钮时,会出现用户表单,但一旦选中一个复选框,就无法选中另一个复选框以取消选中第一个复选框:第一个复选框一直处于选中状态。
我希望我的问题得到清楚的解释,在此先感谢您的帮助,抱歉我的英语问题。
您需要在用户表单中禁用事件。在你的情况下可能看起来像那样
Option Explicit
Dim eventOn As Boolean
Private Sub CheckBox2_Click()
If eventOn Then
eventOn = False
If CheckBox2.Value = True And CheckBox3.Value = True Then
CheckBox3.Value = False
TextBox8.Enabled = True
TextBox8.BackColor = RGB(255, 255, 255)
Else
CheckBox3.Value = False
CheckBox2.Value = True
End If
eventOn = True
End If
End Sub
Private Sub CheckBox3_Click()
If eventOn Then
eventOn = False
If CheckBox3.Value = True And CheckBox2.Value = True Then
CheckBox2.Value = False
TextBox8.Enabled = False
TextBox8.BackColor = RGB(205, 205, 205)
Else
CheckBox2.Value = False
CheckBox3.Value = True
TextBox8.Enabled = False
End If
eventOn = True
End If
End Sub
Private Sub UserForm_Initialize()
eventOn = True
End Sub
进一步阅读请看ChipPearson article on Surpress Change in Forms or disabling-events-in-userforms
我目前正在 VBA 中进行一个小项目,因此我创建了一个带有两个复选框的用户窗体,并为这些复选框编写了 2 个子项,以确保只有一个复选框可以检查。这是 2 个复选框的代码:
Private Sub CheckBox2_Click()
If CheckBox2.Value = True And CheckBox3.Value = True Then
CheckBox3.Value = False
TextBox8.Enabled = True
TextBox8.BackColor = RGB(255, 255, 255)
Else
CheckBox3.Value = False
CheckBox2.Value = True
End If
End Sub
Private Sub CheckBox3_Click()
If CheckBox3.Value = True And CheckBox2.Value = True Then
CheckBox2.Value = False
TextBox8.Enabled = False
TextBox8.BackColor = RGB(205, 205, 205)
Else
CheckBox2.Value = False
CheckBox3.Value = True
TextBox8.Enabled = False
End If
End Sub
如您所见,subs 还 enable/disables 考虑到选中复选框的 TextBox 字段。
当我尝试使用 运行 按钮从主菜单启动用户窗体时,此代码完美运行:如果您选中一个复选框,另一个将被取消选中。
我还在我的 Excel 文件的第一个 sheet 上创建了一个启动用户窗体的按钮,这样用户就不必激活开发人员菜单来启动它。这是附加到此按钮的子项:
Private Sub lance_interface()
UF.Show
End Sub
当我单击此按钮时,会出现用户表单,但一旦选中一个复选框,就无法选中另一个复选框以取消选中第一个复选框:第一个复选框一直处于选中状态。
我希望我的问题得到清楚的解释,在此先感谢您的帮助,抱歉我的英语问题。
您需要在用户表单中禁用事件。在你的情况下可能看起来像那样
Option Explicit
Dim eventOn As Boolean
Private Sub CheckBox2_Click()
If eventOn Then
eventOn = False
If CheckBox2.Value = True And CheckBox3.Value = True Then
CheckBox3.Value = False
TextBox8.Enabled = True
TextBox8.BackColor = RGB(255, 255, 255)
Else
CheckBox3.Value = False
CheckBox2.Value = True
End If
eventOn = True
End If
End Sub
Private Sub CheckBox3_Click()
If eventOn Then
eventOn = False
If CheckBox3.Value = True And CheckBox2.Value = True Then
CheckBox2.Value = False
TextBox8.Enabled = False
TextBox8.BackColor = RGB(205, 205, 205)
Else
CheckBox2.Value = False
CheckBox3.Value = True
TextBox8.Enabled = False
End If
eventOn = True
End If
End Sub
Private Sub UserForm_Initialize()
eventOn = True
End Sub
进一步阅读请看ChipPearson article on Surpress Change in Forms or disabling-events-in-userforms