+ Reply to Thread
Results 1 to 5 of 5

Command Button True/False States

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    06-21-2010
    Location
    -
    MS-Off Ver
    Excel 2010
    Posts
    1,211

    Command Button True/False States

    How would I go about setting the 'value' of a Form Command Button to either True or False, so that say it's initial value is set to False and when clicked, it's value changes to True and then back to False again when clicked again.

    Many thanks

  2. #2
    Forum Expert Colin Legg's Avatar
    Join Date
    03-30-2008
    Location
    UK
    MS-Off Ver
    365
    Posts
    1,256

    Re: Command Button True/False States

    By "Form" do you mean a Forms button, or do you mean an ActiveX commandbutton on a Userform?

    I'll assume the latter. Set the Caption to False in the properties window. Then this is the click event handler code:
    Private Sub CommandButton1_Click()
        CommandButton1.Caption = CStr(Not (CBool(CommandButton1.Caption)))
    End Sub
    But are you sure that a commandbutton it the correct control to use? How about a checkbox?
    Hope that helps,

    Colin

    RAD Excel Blog

  3. #3
    Forum Contributor
    Join Date
    06-21-2010
    Location
    -
    MS-Off Ver
    Excel 2010
    Posts
    1,211

    Re: Command Button True/False States

    Hi Colin

    I actually meant a Form Command Button rather than an Active X one. It is the 'State' rather than the caption that I need to change to either True or False, the logic being that when the 'State is 'True, it will show something and when clicked again, the 'State' should change to 'False', to hide the object. I hope that makes sense... The button text needs to change also...

  4. #4
    Forum Expert Colin Legg's Avatar
    Join Date
    03-30-2008
    Location
    UK
    MS-Off Ver
    365
    Posts
    1,256

    Re: Command Button True/False States

    Hi Hangman,

    It sounds like a checkbox would be a more logical choice of control?

    I might have misunderstood what you mean by 'state', but I've attached an example where there are two forms buttons on a worksheet. Clicking on button 1 toggles its caption between True / False and shows/hides the other button. The code is:
    Sub Button1_Click()
    
        Dim btnCaller As Button
        Dim blnNewState As Boolean
        
        Set btnCaller = Sheet1.Buttons(Application.Caller)
    
        blnNewState = Not CBool(btnCaller.Caption)
        btnCaller.Caption = CStr(blnNewState)
        
        Sheet1.Buttons("Button 3").Visible = blnNewState
        
    End Sub
    Attached Files Attached Files

  5. #5
    Forum Contributor
    Join Date
    06-21-2010
    Location
    -
    MS-Off Ver
    Excel 2010
    Posts
    1,211

    Re: Command Button True/False States

    Many thanks Colin. I'll take a look and see if this will work for what I'm trying to do...

    On first view it looks as though it will. Thank you for taking the time to look at this. I'll report back tomorrow.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1