Home |
Search |
Today's Posts |
|
#1
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
I am hoping someone might be able to point me in the right direction
regarding a feature I have no idea what syntax to use, or where to place it. I have a form created in VBE. Within this Form is a Frame that has three Check Boxes. Also on the Form, but not in the Frame, I have a Command Button. I want the Command Button to be disabled unless all three Check Boxes are ticked. Any help would be very gratefully received. |
#2
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
Hi Phil
The code below should do what you want, to test I set up a userform with a frame containing the three checkboxes then added a button. what the code does is it has a counter that you add one to if a checkbox is checked and subtract one if the checkbox is blank, ten when the counter reaches three the button will be enabled. Option Explicit Dim Ctrl As MSForms.Control Dim i, CountChks As Integer Private Sub CheckBox1_Click() i = 1 CheckYourChecks End Sub Private Sub CheckBox2_Click() i = 2 CheckYourChecks End Sub Private Sub CheckBox3_Click() i = 3 CheckYourChecks End Sub Private Sub UserForm_Initialize() i = 0 CountChks = 0 CommandButton1.Enabled = False End Sub Sub CheckYourChecks() Set Ctrl = UserForm1.Controls("CheckBox" & i) If Ctrl.Value = True Then CountChks = CountChks + 1 Else CountChks = CountChks - 1 End If If CountChks = 3 Then CommandButton1.Enabled = True Else CommandButton1.Enabled = False End If End Sub Hope this helps S |
#3
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
You can use a bit flag, so if you need to vary the number of check boxes, it
is more easy to maintain. Whilst this code is not the most compact, it shows the logic of the procedure clearly. Dim Flag As Long Const ALLSET As Long = 2 ^ 0 + 2 ^ 1 + 2 ^ 2 Private Sub CheckBox1_Click() If CheckBox1.Value = True Then Flag = Flag Or 2 ^ 0 Else Flag = Flag And Not (2 ^ 0) End If Call CheckStatus End Sub Private Sub CheckBox2_Click() If CheckBox2.Value = True Then Flag = Flag Or 2 ^ 1 Else Flag = Flag And Not (2 ^ 1) End If Call CheckStatus End Sub Private Sub CheckBox3_Click() If CheckBox3.Value = True Then Flag = Flag Or 2 ^ 2 Else Flag = Flag And Not (2 ^ 2) End If Call CheckStatus End Sub Private Sub CheckStatus() CommandButton2.Enabled = ((Flag And ALLSET) = ALLSET) End Sub Private Sub UserForm_Initialize() Flag = 0 Call CheckStatus End Sub NickHK "Phil" wrote in message ... I am hoping someone might be able to point me in the right direction regarding a feature I have no idea what syntax to use, or where to place it. I have a form created in VBE. Within this Form is a Frame that has three Check Boxes. Also on the Form, but not in the Frame, I have a Command Button. I want the Command Button to be disabled unless all three Check Boxes are ticked. Any help would be very gratefully received. |
#4
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
Many Thanks to you both for your help. It is much appreciated.
"NickHK" wrote: You can use a bit flag, so if you need to vary the number of check boxes, it is more easy to maintain. Whilst this code is not the most compact, it shows the logic of the procedure clearly. Dim Flag As Long Const ALLSET As Long = 2 ^ 0 + 2 ^ 1 + 2 ^ 2 Private Sub CheckBox1_Click() If CheckBox1.Value = True Then Flag = Flag Or 2 ^ 0 Else Flag = Flag And Not (2 ^ 0) End If Call CheckStatus End Sub Private Sub CheckBox2_Click() If CheckBox2.Value = True Then Flag = Flag Or 2 ^ 1 Else Flag = Flag And Not (2 ^ 1) End If Call CheckStatus End Sub Private Sub CheckBox3_Click() If CheckBox3.Value = True Then Flag = Flag Or 2 ^ 2 Else Flag = Flag And Not (2 ^ 2) End If Call CheckStatus End Sub Private Sub CheckStatus() CommandButton2.Enabled = ((Flag And ALLSET) = ALLSET) End Sub Private Sub UserForm_Initialize() Flag = 0 Call CheckStatus End Sub NickHK "Phil" wrote in message ... I am hoping someone might be able to point me in the right direction regarding a feature I have no idea what syntax to use, or where to place it. I have a form created in VBE. Within this Form is a Frame that has three Check Boxes. Also on the Form, but not in the Frame, I have a Command Button. I want the Command Button to be disabled unless all three Check Boxes are ticked. Any help would be very gratefully received. |
Reply |
Thread Tools | Search this Thread |
Display Modes | |
|
|
![]() |
||||
Thread | Forum | |||
how to disable/enable command button(ActiveX) using macro | Excel Discussion (Misc queries) | |||
enable/disable button based on cell value | Excel Discussion (Misc queries) | |||
Enable / Disable a button using a macro | Excel Programming | |||
How to enable/disable a button. | Excel Programming | |||
Enable/Disable Button | Excel Programming |