Home » excel » Disable/Enable button in Excel/VBA

Disable/Enable button in Excel/VBA

Posted by: admin March 9, 2020 Leave a comment

Questions:

I’m trying the following function in 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!

How to&Answers:

The following works for me (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 = False does not gray out a button.

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

Answer:

… 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:

Sub test()

   Sheets(1).CommandButton1.Enabled = False

End Sub

Hope this helps…

Answer:

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

Answer:

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

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.

Answer:

Others are correct in saying that setting button.enabled = false doesn’t prevent the button from triggering. However, I found that setting button.visible = false does work. The button disappears and can’t be clicked until you set visible to true again.

Answer:

This is what iDevelop is trying to say Enabled Property

So you have been infact using enabled, coz your initial post was 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