Excel/VBA 中的禁用/启用按钮

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14123017/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 14:39:51  来源:igfitidea点击:

Disable/Enable button in Excel/VBA

excelvbaexcel-vba

提问by Jonathan V

I'm trying the following function in VBA/Excel:

我正在 VBA/Excel 中尝试以下功能:

Sub function_name()
  button.enabled=false
  Call Long_Function       ' duration: 10sec
  button.enabled=true
End Sub

For some reason, this button disabling does not work (it stays enabled in the excel work sheet) I tried experimenting with DoEvents and delays, but no luck there. Any ideas? Thanks!

出于某种原因,此按钮禁用不起作用(它在 excel 工作表中保持启用状态)我尝试尝试使用 DoEvents 和延迟,但没有成功。有任何想法吗?谢谢!

回答by Axel Kemper

The following works for me (Excel 2010)

以下对我有用(Excel 2010)

Dim b1 As Button

Set b1 = ActiveSheet.Buttons("Button 1")

b1.Font.ColorIndex = 15
b1.Enabled = False
Application.Cursor = xlWait
Call aLongAction
b1.Enabled = True
b1.Font.ColorIndex = 1
Application.Cursor = xlDefault

Be aware that .enabled = Falsedoes not gray out a button.

请注意,.enabled = False这不会使按钮变灰。

The font color has to be set explicitely to get it grayed.

必须明确设置字体颜色才能使其变灰。

回答by John Bustos

... I don't know if you're using an activex button or not, but when I insert an activex button into sheet1 in Excel called CommandButton1, the following code works fine:

...我不知道您是否使用了 activex 按钮,但是当我将一个 activex 按钮插入到 Excel 中名为 CommandButton1 的 sheet1 中时,以下代码可以正常工作:

Sub test()

   Sheets(1).CommandButton1.Enabled = False

End Sub

Hope this helps...

希望这可以帮助...

回答by mansi

too good !!! it's working and resolved my one day old problem easily

太好了 !!!它正在工作并轻松解决了我一天的问题

Dim b1 As Button

Set b1 = ActiveSheet.Buttons("Button 1")


b1.Enabled = False

回答by Jonaithan

This is working for me (Excel 2016) with a new ActiveX button, assign a control to you button and you're all set.

这对我(Excel 2016)有用,有一个新的 ActiveX 按钮,为你的按钮分配一个控件,你就完成了。

Sub deactivate_buttons()

     ActiveSheet.Shapes.Item("CommandButton1").ControlFormat.Enabled = False

End Sub

It changes the "Enabled" property in the ActiveX button Properties box to False and the button becomes inactive and greyed out.

它将 ActiveX 按钮属性框中的“已启用”属性更改为 False,并且按钮变为非活动状态并呈灰色。

回答by user3442395

Others are correct in saying that setting button.enabled = falsedoesn't prevent the button from triggering. However, I found that setting button.visible = falsedoes work. The button disappears and can't be clicked until you set visibleto trueagain.

其他人说设置button.enabled = false不会阻止按钮触发是正确的。但是,我发现设置button.visible = false确实有效。该按钮会消失并且无法点击,直到您再次设置visibletrue

回答by bonCodigo

This is what iDevelop is trying to say Enabled Property

这就是 iDevelop 试图说的启用属性

So you have been infact using enabled, coz your initial post was enable..

所以你enabled实际上一直在使用,因为你最初的帖子是enable..

You may try the following:

您可以尝试以下操作:

Sub disenable()
  sheets(1).button1.enabled=false
  DoEvents
  Application.ScreenUpdating = True

  For i = 1 To 10
    Application.Wait (Now + TimeValue("0:00:1"))
  Next i

  sheets(1).button1.enabled = False
End Sub