有没有办法同时禁用多个按钮?
我有一个 vba 用户窗体,上面有 36 个按钮。当我的电子表格之一上的值达到特定数字时,我想禁用所有按钮。现在,每次单击按钮,我引用的电子表格上的数字就会增加一。当数量达到三个时,我想禁用所有按钮。
I have a vba userForm that has 36 buttons on it. I would like to disable all buttons when a value on one of my spreadshets reaches a certain number. Right now with each click of a button a number goes up by one on the spreadsheet I'm referencing. When the number reaches three I would like to disable all buttons.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您不想禁用其他按钮,解决方案是使用 Tag 属性。将您想要同时启用或禁用的所有按钮的 Tag 属性设置为相同的值。然后您可以检查该值并启用/禁用它们。另一种方法是将它们命名为相同的前缀或后缀,并在代码中进行检查。
添加
顺便说一句,Control 对象没有 Enabled 属性。因此,您必须将其“投射”到 CommandButton 才能禁用它。显然,Control 对象确实具有 Enabled 属性,但它不会在智能感知中显示。但是,您仍然应该尝试将 Control 转换为 CommandButton,以确保这就是您所拥有的。这是一个扩展版本:If you have other buttons which you do not want disabled, the solution is to use the Tag property. Set the Tag property on all the buttons which you will want to enable or disable together to the same value. Then you can check for that value in a look and enable/disable them. Another way is to name them the same prefix or suffix and check for that in your code.
Addition
Btw, the Control object does not have an Enabled property. So you must "cast" it to a CommandButton to disable it. Apparently a Control object does have an Enabled property but it does not show in intellisense. However, you should still try to cast the Control to a CommandButton to ensure that's what you have. Here's an expanded version:将所有按钮放在一个 Frame 对象中,然后禁用整个框架。这也将禁用框架内的所有内容。
或者,根据您的最后一个问题,您可以使用以下代码:
理想情况下,您希望像托马斯建议的那样将 Control 对象强制转换为按钮。
Place all the buttons in a Frame object, then just disable the entire frame. This will also disable everything inside the frame.
Alternatively, based on your last question, you could use this code:
Ideally, you would want to cast the Control objects to Buttons like Thomas suggests.
@Mike:尝试 --
(将
Sheet1
、A1
、UserForm1
和CommandButton1
替换为适合您工作簿的正确值)@Mike: Try --
(Replace
Sheet1
,A1
,UserForm1
andCommandButton1
with the correct ones for your workbook)