Consulting

Results 1 to 8 of 8

Thread: Solved: Setting CommandButton = False under a specific condition

  1. #1

    Solved: Setting CommandButton = False under a specific condition

    My knowledge of Excel VBA is growing with the help of this forum. Thanks!

    Now I am really getting brave, you know a little bit of knowledge is dangerous. What I am trying to accomplish is to remove the ability of the user to click on a commandbutton prior to entering data into a textbox. So while TextBox1 = "" CommandButton3 = False.

    I'm thinking that when the userform is initialized CommandButton3 would be set to False

    Is this correct? But I have no idea of what the code should be.

  2. #2
    Moderator VBAX Wizard lucas's Avatar
    Joined
    Jun 2004
    Location
    Tulsa, Oklahoma
    Posts
    7,323
    Location
    One example:

    for the initialize:
    [vba]Private Sub UserForm_Initialize()
    CommandButton1.Enabled = False
    End Sub
    [/vba]

    for the textbox:
    [vba]Private Sub Textbox1_Change()
    If TextBox1.Value <> "" Then
    CommandButton1.Enabled = True
    Else
    CommandButton1.Enabled = False
    End If
    End Sub
    [/vba]
    Steve
    "Nearly all men can stand adversity, but if you want to test a man's character, give him power."
    -Abraham Lincoln

  3. #3
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Steve, keep it mean

    [vba]

    Private Sub Textbox1_Change()
    CommandButton1.Enabled = TextBox1.Value <> ""
    End Sub
    [/vba]
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  4. #4
    Moderator VBAX Wizard lucas's Avatar
    Joined
    Jun 2004
    Location
    Tulsa, Oklahoma
    Posts
    7,323
    Location
    Well of course. Lean and mean is always better...

    Thanks Bob.
    Steve
    "Nearly all men can stand adversity, but if you want to test a man's character, give him power."
    -Abraham Lincoln

  5. #5
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Meaner?
    [VBA]
    CommandButton1.Enabled = Len(TextBox1)

    [/VBA]
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  6. #6
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Quote Originally Posted by mdmackillop
    Meaner?
    [VBA]
    CommandButton1.Enabled = Len(TextBox1)

    [/VBA]
    Positively austere! Although I think it is cheating a bit by using the default property (you know my views on property defaults!).
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  7. #7
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,728
    Location
    All lean, mean, and austere solutions are indeed elegant, but Lucas' approach has --- IMHO --- the advantage of being easier to decypher in a year or two when you come back to it

    Paul

  8. #8
    Wow! It is great to have options. However, being a VBA sub-neophyte I went with the code by Lucas. I need the practice of writing and tracing the code. I'll spend today applying this new knowledge to all the other buttons on my userform. Thanks to you all!!!!

Posting Permissions

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