+ Reply to Thread
Results 1 to 4 of 4

Restricting command button action in a user form

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    11-20-2009
    Location
    Melbourne
    MS-Off Ver
    Excel 365
    Posts
    288

    Restricting command button action in a user form

    In my Userform I would like to stop a command button from being pressed until a minimum of 5 checkboxes out of 10 have been selected.
    I have a total of how many checkboxes have been selected. How do I write the code on the command button that if Label X (which has the total of the checkboxes) is greater than 4 then run the Comman Button to exit Excel.
    Thank you for your help.

  2. #2
    Forum Guru :) Sixthsense :)'s Avatar
    Join Date
    01-01-2012
    Location
    India>Tamilnadu>Chennai
    MS-Off Ver
    2003 To 2010
    Posts
    12,788

    Re: Restricting command button action in a user form

    Use a textbox instead of label and try to code something like this...

    Private Sub CheckBox1_Click()
        abc = IIf(CheckBox1.Value = True, 1, 0) + IIf(CheckBox2.Value = True, 1, 0) + IIf(CheckBox3.Value = True, 1, 0)
        TextBox1.Value = abc
    End Sub
    Private Sub CheckBox2_Click()
        abc = IIf(CheckBox1.Value = True, 1, 0) + IIf(CheckBox2.Value = True, 1, 0) + IIf(CheckBox3.Value = True, 1, 0)
        TextBox1.Value = abc
    End Sub
    Private Sub CheckBox3_Click()
        abc = IIf(CheckBox1.Value = True, 1, 0) + IIf(CheckBox2.Value = True, 1, 0) + IIf(CheckBox3.Value = True, 1, 0)
        TextBox1.Value = abc
    End Sub
    Private Sub TextBox1_Change()
        If TextBox1.Value > 2 Then
            CommandButton1.Enabled = True
        Else
            CommandButton1.Enabled = False
        End If
    End Sub


    If your problem is solved, then please mark the thread as SOLVED>>Above your first post>>Thread Tools>>
    Mark your thread as Solved


    If the suggestion helps you, then Click *below to Add Reputation

  3. #3
    Forum Expert JapanDave's Avatar
    Join Date
    06-10-2008
    Location
    The grid, I got in!
    MS-Off Ver
    Excel 2010/13
    Posts
    1,696

    Re: Restricting command button action in a user form

    You could do it this way.

    Private Sub CheckBox1_Click()
       Dim cBox As MSForms.Control
       Dim y As Long
       
          y = 0
        For Each cBox In Me.Controls
            If TypeName(cBox) = "CheckBox" Then
              If cBox = True Then
               y = y + 1
                 If y > 4 Then
                   Me.CommandButton1.Enabled = 1
                 End If
              End If
            End If
        Next cBox
    End Sub
    
    Private Sub UserForm_Initialize()
    
      Me.CommandButton1.Enabled = 0
      
    End Sub
    Be fore warned, I regularly post drunk. So don't take offence (too much) to what I say.
    I am the real 'Napster'
    The Grid. A digital frontier. I tried to picture clusters of information as they moved through the computer. What did they look like? Ships? motorcycles? Were the circuits like freeways? I kept dreaming of a world I thought I'd never see. And then, one day...

    If you receive help please give thanks. Click the * in the bottom left hand corner.

    snb's VBA Help Files

  4. #4
    Forum Expert
    Join Date
    08-12-2012
    Location
    Sydney, Australia
    MS-Off Ver
    Excel 2010
    Posts
    5,636

    Re: Restricting command button action in a user form

    my variation

    did not do proper naming but you get the idea
    Attached Files Attached Files

+ 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