vba 有没有办法一次禁用多个按钮?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5597889/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Is there a way to disable multiple buttons at one time?
提问by Mike
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.
我有一个 vba userForm,上面有 36 个按钮。当我的一个电子表格上的值达到某个数字时,我想禁用所有按钮。现在,每次单击一个按钮,我所引用的电子表格上的数字就会增加一个。当数字达到三个时,我想禁用所有按钮。
采纳答案by Thomas
Dim oCtrl As Control
Dim oButton As CommandButton
For Each oCtrl In MyForm.Controls
If oCtrl.Tag = "SomeValue" Then
Set oButton = oCtrl
oButton.Enabled = False
End If
Next
Set oCtrl = Nothing
Set oButton = Nothing
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.
如果您有其他不想禁用的按钮,解决方案是使用 Tag 属性。将要一起启用或禁用的所有按钮上的 Tag 属性设置为相同的值。然后您可以查看该值并启用/禁用它们。另一种方法是将它们命名为相同的前缀或后缀,并在您的代码中进行检查。
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:
顺便说一句, Control 对象没有 Enabled 属性。因此,您必须将其“投射”到 CommandButton 以禁用它。显然 Control 对象确实有一个 Enabled 属性,但它没有在智能感知中显示。但是,您仍应尝试将 Control 强制转换为 CommandButton,以确保您拥有该控件。这是一个扩展版本:
Dim oCtrl As Control
Dim oButton As CommandButton
For Each oCtrl In MyForm.Controls
If oCtrl.Tag = "SomeValue" Then
On Error Resume Next
Set oButton = oCtrl
On Error GoTo 0
If Not oButton Is Nothing Then
oButton.Enabled = False
End If
End If
Next
Set oCtrl = Nothing
Set oButton = Nothing
回答by Stewbob
Place all the buttons in a Frame object, then just disable the entire frame. This will also disable everything inside the frame.
将所有按钮放在一个 Frame 对象中,然后禁用整个框架。这也将禁用框架内的所有内容。
Alternatively, based on your last question, you could use this code:
或者,根据您的最后一个问题,您可以使用以下代码:
Dim counter As Integer
Private Sub btn1_Click()
CaptureImage (btn1.Name)
End Sub
Private Sub btn2_Click()
CaptureImage (btn2.Name)
End Sub
Private Sub btn3_Click()
CaptureImage (btn3.Name)
End Sub
Private Sub UserForm_Activate()
counter = 1
End Sub
Private Sub CaptureImage(ByVal btnName As String)
Controls("capture" & counter).Picture = Controls(btnName).Picture
counter = counter + 1
If counter > 3 Then
DisableButtons
End If
End Sub
Private Sub DisableButtons()
Dim ctl As Control
For Each ctl In UserForm1.Controls
If Left(ctl.Name, 3) = "btn" Then
ctl.Enabled = False
End If
Next
End Sub
Ideally, you would want to cast the Control objects to Buttons like Thomas suggests.
理想情况下,您希望像 Thomas 建议的那样将 Control 对象转换为 Buttons。
回答by stealthyninja
@Mike: Try --
@Mike:试试——
If Sheets("Sheet1").Range("A1").Value = 3 Then
UserForm1.CommandButton1.Enabled = True
Else
UserForm1.CommandButton1.Enabled = False
End If
(Replace Sheet1
, A1
, UserForm1
and CommandButton1
with the correct ones for your workbook)
(替换Sheet1
,A1
,UserForm1
并CommandButton1
用正确的人对你的工作簿)