Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Disable/Enable button in Excel/VBA

Tags:

excel

vba

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!

like image 572
Jonathan V Avatar asked Jan 02 '13 13:01

Jonathan V


People also ask

What is enable or disable button based on cell value in Excel?

Right click the sheet that contain the button, and then choose View Code from the context menu, in the Microsoft Visual Basic for Applications window, please copy and paste the below code into the blank module, see screenshot: VBA code: Enable or disable a button based on two cell values: Private Sub Worksheet_Change( ...


1 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.

like image 132
Axel Kemper Avatar answered Oct 18 '22 05:10

Axel Kemper