我正在尝试在VBA/Excel中使用以下函数:
Sub function_name()
button.enabled=false
Call Long_Function ' duration: 10sec
button.enabled=true
End Sub
由于某种原因,此按钮禁用无效(在Excel工作表中仍处于启用状态)。 我尝试使用DoEvents和延迟进行实验,但没有成功。 有什么想法吗? 谢谢!
以下内容适用于我所使用的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
请注意,.enabled = False
不会使按钮变灰。
必须显式设置字体颜色才能使其变灰。我不知道你是否在使用ActiveX按钮,但是当我在Excel的Sheet1中插入一个名为CommandButton1的ActiveX按钮时,以下代码可以正常工作:
Sub test()
Sheets(1).CommandButton1.Enabled = False
End Sub
太好了!!!它正在工作并轻松解决了我一天的问题
Dim b1 As Button
Set b1 = ActiveSheet.Buttons("Button 1")
b1.Enabled = False
这对我来说可行(Excel 2016),使用一个新的ActiveX按钮,将控件分配给您的按钮,然后您就可以开始了。
Sub deactivate_buttons()
ActiveSheet.Shapes.Item("CommandButton1").ControlFormat.Enabled = False
End Sub
我正在使用 Excel 2010,以下 VBA 代码适用于表单按钮。
它可以删除按钮上分配的宏,并在下一个命令中重新分配。
要禁用:
ActiveSheet.Shapes("Button Name").OnAction = Empty
ActiveSheet.Shapes("Button Name").DrawingObject.Font.ColorIndex = 16
启用:
ActiveSheet.Shapes("Button Name").OnAction = ActiveWorkbook.Name & "!Macro function Name with _Click"
ActiveSheet.Shapes("Button Name").DrawingObject.Font.ColorIndex = 1
这就是iDevelop想要表达的启用属性。
所以,你实际上一直在使用enabled
,因为你最初的帖子是enable
。
你可以尝试以下方法:
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
b1.Enabled = False
并不能阻止与该按钮相关联的操作在单击时发生。 - Kes Perron