2

I am trying to disable a button placed on my sheet1 once the user click on it. I went through few old Stackoverflow answers but dosent work as my expectation.

The code:

Sub Button2_Click()

    Call FindADate

    Dim myshape As Shape: Set myshape = ThisWorkbook.Worksheets("Sheet1").Shapes("Button 2")
    With myshape
        .ControlFormat.Enabled = False           '---> Disable the button
        .TextFrame.Characters.Font.ColorIndex = 15 '---> Grey out button label
    End With
End Sub

It actually grayed out the button which give a feel that the button is disabled but User can click again and again and it run my code.

Kindly let me know a solution to get the button disabled after 1 click, the button will be active again only once I close and re open the excel. (I am using MS Office professional Plus 2013)

Thanks in advance.

2
  • why not keep a variable to remember that it has been clicked? Commented Dec 5, 2017 at 23:26
  • Thanks @S Meaden will you show me how to do that Commented Dec 5, 2017 at 23:28

3 Answers 3

4

You need to record somehow not to process the click. You could examine the button's text colour and if grey then ignore the click. Alternatively, you could set a variable either a global, module or static local variable. I chose the static local variable in the code below, called vDisable.

Option Explicit

Sub Button2_Click()
    Static vDisable As Variant
    If IsEmpty(vDisable) Then
        
        Call FindADate
    
        Dim myshape As Shape: Set myshape = ThisWorkbook.Worksheets("Sheet1").Shapes("Button 2")
        With myshape
            .ControlFormat.Enabled = False           '---> Disable the button
            .TextFrame.Characters.Font.ColorIndex = 15 '---> Grey out button label
        End With
        vDisable = True
    End If
End Sub
Sign up to request clarification or add additional context in comments.

Comments

0

after Dim line just write:

if myshape.TextFrame.Characters.Font.ColorIndex = 15 then exit sub

that should be enough

Comments

0

I know this is an old question, but I had the same situation and solved it by the following:

With myshape
     .OnAction = ""           '---> Removes any macro from the button
     .TextFrame.Characters.Font.ColorIndex = 15 '---> Grey out button label
End With

This removes the need for a global/static variable.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.