Error Checking in Excel
Thanks Thanks:  0
Likes Likes:  0
Page 1 of 2 12 LastLast
Results 1 to 10 of 16

Thread: Change Button Color Upon Click

  1. #1
    Board Regular
    Join Date
    Aug 2008
    Posts
    427
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default Change Button Color Upon Click

    Hi guys,

    I was wondering if it is possible to change the color of a command button after it's been clicked?

    This is to allow the user to identify which buttons have already been clicked.

    Thanks!

  2. #2
    MrExcel MVP Tom Urtis's Avatar
    Join Date
    Feb 2002
    Location
    San Francisco, California USA
    Posts
    11,168
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default Re: Change Button Color Upon Click

    You can always stick the instructions in the click event:

    Private Sub CommandButton1_Click()
    With CommandButton1
    .Caption = "I've already been clicked"
    .BackColor = &HFFFF&
    .ForeColor = &H0&
    End With
    End Sub

  3. #3
    Board Regular
    Join Date
    Aug 2008
    Posts
    427
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default Re: Change Button Color Upon Click

    Perfect. Just what I needed.
    Thanks.

  4. #4
    Board Regular
    Join Date
    Aug 2008
    Posts
    427
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default Re: Change Button Color Upon Click

    Hmm I found a loophole..
    If I'm running an inputbox and I click cancel, the button changes colour anyway.

    Any idea or alternatives I could use?

  5. #5
    Board Regular
    Join Date
    Feb 2007
    Location
    Oregon
    Posts
    91
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default Re: Change Button Color Upon Click

    Is the command button calling the input box?

  6. #6
    Board Regular
    Join Date
    Aug 2008
    Posts
    427
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default Re: Change Button Color Upon Click

    Yes. Allow me to explain myself better.
    In the command button, I call up some other subs.
    In those subs, I have input boxes.

    The code that Tom gave would change the colour as long as the user has clicked the button... So if the user clicks the button, the sub runs, and the input box comes out. If the user clicks cancel, it should exit entirely. Colour should not change.

    Do you get what I'm trying to say?

  7. #7
    Board Regular
    Join Date
    Feb 2007
    Location
    Oregon
    Posts
    91
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default Re: Change Button Color Upon Click

    Sure, you could reset the command button's properties when cancel is clicked. Try something like this within the sub being called from the commandbutton:

    (set the caption, backcolor, and forecolor as needed)
    Code:
    msg = Application.InputBox("Your message")
    If msg = False Then
        With Sheet1.CommandButton1
            .Caption = ""
            .BackColor = ""
            .ForeColor = ""
        End With
        Exit Sub
    End If

  8. #8
    Board Regular
    Join Date
    Aug 2008
    Posts
    427
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default Re: Change Button Color Upon Click

    Hi,

    That would work if I'm putting my code in the command button.
    But I'm calling a couple of other subs from my command button. That's why it's a bit complicated. =/

  9. #9
    Board Regular
    Join Date
    Feb 2007
    Location
    Oregon
    Posts
    91
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default Re: Change Button Color Upon Click

    Put that code in the sub that calls the inputbox, not the commandbutton code.

    What is you sub currently doing if the user clicks cancel?

  10. #10
    Board Regular
    Join Date
    Aug 2008
    Posts
    427
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default Re: Change Button Color Upon Click

    I've tried putting the code in the sub that calls the inputbox. It doesn't work.
    The sub continues to open a file and autofilter some stuff

Some videos you may like

User Tag List

Like this thread? Share it with others

Like this thread? Share it with others

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •