简体   繁体   English

如何使用 VBA Excel 中的复选框启用/禁用命令按钮

[英]How to enable/disable CommandButtons by using checkboxes in VBA Excel

The aim is if the checkbox next to the commandbutton "Examine 1" is activated manually the commandbutton "Examine 1" are supposed to be deactivated.目的是如果手动激活命令按钮“检查 1”旁边的复选框,则应该停用命令按钮“检查 1”。 Which means that if the overall Button "Execute all" is pushed then all other buttons below will be triggered one by one except the "Examine 1" Button.这意味着如果整个按钮“全部执行”被按下,那么除了“检查 1”按钮之外,下面的所有其他按钮都将被一一触发。 I have already written the Sub for the "Examine 1" Button and also for the checkBox 1. I just dont know how to get the Sub of the checkbox integrated into the "Examine 1" Sub.我已经为“检查 1”按钮和复选框 1 编写了子程序。我只是不知道如何将复选框的子程序集成到“检查 1”子程序中。 Currently, if I push the overall BUtton "Execute all" then the Button "Examine 1" is also executed no matter whether the checkbox is marked or not.目前,如果我按下整体按钮“全部执行”,那么无论是否标记复选框,都会执行按钮“检查 1”。

This is the macro of the "Examine 1" Button这是“检查 1”按钮的宏

Sub Examine_Click()
....
...

This one is Sub for the checkbox which refers to the Sub "Examine_Click()"这是复选框的 Sub ,它指的是 Sub "Examine_Click()"

Private Sub CheckBox1_Click()

Dim b As Button

Set b = ActiveSheet.Buttons("Button 4")

If CheckBox1.Value = True Then
   b.Enabled = True
   b.Font.ColorIndex = 1
Else: b.Enabled = False
    b.Font.ColorIndex = 15

End If
End Sub

In your Checkbox code you are never enabling the button: If CheckBox1.Value = True Then: b.Enabled = False |在您的复选框代码中,您永远不会启用按钮: If CheckBox1.Value = True Then: b.Enabled = False | Else: b.Enabled = False . Else: b.Enabled = False But that's not the problem.但这不是问题。

You state that the ExecuteAll button is supposed to call the Subs associated with the other buttons but I'm not sure that the b.Enabled property will prevent the Sub call from running, it will only disable the user's click.您声明 ExecuteAll 按钮应该调用与其他按钮关联的 Subs,但我不确定b.Enabled属性是否会阻止 Sub 调用运行,它只会禁用用户的单击。 In the ExecuteAll_Click() you should test the relevant checkbox before executing the associated button's code.ExecuteAll_Click()您应该在执行相关按钮的代码之前测试相关的复选框。

the solution is:解决办法是:

 If Worksheets("Control").CheckBox1.Value = True Then

This means that if the checkbox is activated the main macro starts running, otherwise it jumps stright to "end sub"这意味着如果复选框被激活,主宏开始运行,否则它会直接跳转到“end sub”

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM