Identify if all option buttons are unchecked on a sheet - excel

I have 4 form control option buttons on a sheet. I am trying to set a hard stop before print function if all 4 option buttons have been left unchecked.
I've successfully gotten all my checkboxes (used shapes not form control) to prompt if left blank but am getting nothing for the buttons.
If ActiveSheet.OptionButtons("button3") = Unchecked _
And ActiveSheet.OptionButtons("button20") = Unchecked _
And ActiveSheet.OptionButtons("button45") = Unchecked _
And ActiveSheet.OptionButtons("buttonEpic") = Unchecked Then
MsgBox "Missing"
Cancel = True
End If
End Sub
I also tried = False and .value = false. All buttons are currently unchecked but it's not prompting the message box or the hard stop.
Thanks for any help!

a very simple workaround since I am able to identify the checked option button on the worksheet but not if it's un-checked. I just identify if one of the 4 buttons is checked, do nothing if that's true and cancel if false.
Private Sub Workbook_BeforePrint (Cancel as Boolean)
With ActiveSheet
If .Optionbuttons ("button1").Value = Checked _
Or .Optionbuttons ("button2").Value = Checked _
Or .Optionbuttons ("button3").Value = Checked Then
Exit Sub
Else: Msgbox "Please select an option"
Cancel = True
End If
End With
End Sub

Related

Either of the checkbox can only be true

Situation
I have two form control check boxes. I am trying to write a code that will allow only either of them to be true.
my code is
Sub CheckBox2_Click()
If CheckBox1.Enabled = True Then
CheckBox2.Enabled = False
Else
If CheckBox2.Enabled = True Then
CheckBox1.Enabled = False
End If
End If
End Sub
I have this code in module and have assigned the same macro for both the checkboxes. I get run-time error 424. I beleive this is very basic problem but I unable to dela with it.
Thank you
Please, test the following way. Form check boxes do not have a click event, as ActiveX ones have. You should associate the next sub to both of them. The check boxes I tested, have their names as "Check Box 1" and "Check Box 2". You have to change yours according to the reality in your case, Please, copy the next code in a standard module and then associate it to both used check boxes:
Option Explicit
Sub FormCheckBoxChange()
If ActiveSheet.CheckBoxes(Application.Caller).value = 1 Then
Select Case Application.Caller
Case "Check Box 1": ActiveSheet.CheckBoxes("Check Box 2").value = -4146
Case "Check Box 2": ActiveSheet.CheckBoxes("Check Box 1").value = -4146
End Select
End If
End Sub
Use instead of the used check box names, the appropriate ones for your case.
In case of Form text boxes, their value is not True and False as in case of ActiveX ones. It is 1 and -4146...
Are you sure you want to enable/disable the checkboxes.
Following code makes sure that either one of both boxes is checked.
Public Sub checkbox2_onClick()
Dim oCb1 As Object
Set oCb1 = Table1.Shapes("Checkbox1").OLEFormat.Object
Dim oCb2 As Object
Set oCb2 = Table1.Shapes("Checkbox2").OLEFormat.Object
If oCb2.Value = 1 Then oCb1.Value = 0
End Sub
Public Sub checkbox1_onClick()
Dim oCb1 As Object
Set oCb1 = Table1.Shapes("Checkbox1").OLEFormat.Object
Dim oCb2 As Object
Set oCb2 = Table1.Shapes("Checkbox2").OLEFormat.Object
If oCb1.Value = 1 Then oCb2.Value = 0
End Sub

Masking Password using checkbox in VBA Excel

I have a Text box (PinEntry) and a Check box. The Check box will mask the password when checked. However, it only masks once. I understand the logic of the first subroutine that it should mask entries as long as I am still entering values in the Text box.
Private Sub CheckBox1_Click()
If AccessForm.PinEntry.Value = True Then
AccessForm.PinEntry.PasswordChar = ""
ElseIf AccessForm.PinEntry.Value = False Then
AccessForm.PinEntry.PasswordChar = "*"
Else
AccessForm.PinEntry.PasswordChar = "*"
End If
End Sub
Private Sub PinEntry_Change()
CheckBox1_Click
End Sub
The reason your code will only mask when clicked is because you will only ever enter your final Else statement. You are checking if the PinEntry (Textbox) has a boolean value of True or False, which it never will.
No need to call the checkbox click event when the text in the PinEntry Changes. Passwordchar is a property that only needs to be set on the textbox when you want to enable or disable it. You just need to act when the Checkbox is clicked.
This code will show the pin if the checkbox is checked, and apply the mask if it is unchecked.
Private Sub CheckBox1_Click()
If CheckBox1.Value Then
PinEntry.PasswordChar = ""
Else
PinEntry.PasswordChar = "*"
End If
End Sub

Only allowing one checkbox to be checked for a userform

So I'm making a userform and I have to use make a group of mutually exclusive checkboxes or only allow the user to pick one. "But just use option buttons!" you cry. Two problems with that:
I already have a separate set of option buttons in the userform (I believe you can somehow group them to allow multiple sets but I am unfamiliar with how to actually do this).
My professor specifically wants checkboxes
so I attempted to solve this problem like this
If CheckBoxBar.Value = True And CheckBoxatm.Value = True Then
GoTo Here:
End If
If CheckBoxatm.Value = True And CheckBoxmmHg.Value = True Then
GoTo Here:
End If
If CheckBoxatm.Value = True And CheckBoxpsia.Value = True Then
GoTo Here:
End If
If CheckBoxBar.Value = True And CheckBoxmmHg.Value = True Then
GoTo Here:
End If
If CheckBoxBar.Value = True And CheckBoxpsia.Value = True Then
GoTo Here:
End If
If CheckBoxmmHg.Value = True And CheckBoxpsia.Value = True Then
GoTo Here:
End If
The here leads to a message box that re initializes the userform after the msg box says "You are only allowed to select one" with code like this
Here: MsgBox "You are only allowed to select on pressure unit."
The code "works" but it always goes to the Here: statement despite only picking one of the checkboxes. Can you spot anything wrong?
Thanks for the help!
As stated by Doug Glancy in a comment, your current code is probably missing an Exit Sub before the label Here:, which is therefore allowing your code to fall into the section after the label.
Another way of achieving what you are after is just to have one If statement which checks if more than one CheckBox is checked, and then display the MsgBox if so, e.g. as follows:
If CheckBoxBar.Value + _
CheckBoxatm.Value + _
CheckBoxmmHg.Value + _
CheckBoxpsia.Value < -1 Then
MsgBox "You are only allowed to select one pressure unit."
Exit Sub
End If
Or you can rely on the .Value being the default property of a CheckBox and thus "reduce" that code to:
If CheckBoxBar + CheckBoxatm + CheckBoxmmHg + CheckBoxpsia < -1 Then
MsgBox "You are only allowed to select one pressure unit."
Exit Sub
End If
Note: This method won't work if the .TripleState property of the CheckBox is set to True. (My thanks to Comintern for pointing that out.)
You can make the checkboxes act like option buttons if you override the click function and clear the other boxes (although you can also have none selected).
Private Sub CheckBoxatm_Click()
If Me.Controls("CheckBoxatm").Value = True Then Call ClearOtherValues("CheckBoxatm")
End Sub
Private Sub CheckBoxBar_Click()
If Me.Controls("CheckBoxBar").Value = True Then Call ClearOtherValues("CheckBoxBar")
End Sub
Private Sub CheckBoxmmHg_Click()
If Me.Controls("CheckBoxmmHg").Value = True Then Call ClearOtherValues("CheckBoxmmHg")
End Sub
Private Sub CheckBoxpsia_Click()
If Me.Controls("CheckBoxpsia").Value = True Then Call ClearOtherValues("CheckBoxpsia")
End Sub
Private Function ClearOtherValues(cb As String)
Dim cbPressure() As String, i As Long
cbPressure = Split("CheckBoxBar,CheckBoxatm,CheckBoxmmHg,CheckBoxpsia", ",")
For i = 0 To UBound(cbPressure)
If cbPressure(i) <> cb Then Me.Controls(cbPressure(i)).Value = False
Next i
End Function

Excel activex check box to enable or disable printing

hie
am looking for a vba code for controlling printing of excel sheet using activex check box in excel.
The code should only print the excel sheet when the checkbox is checked
and disable printing when unchecked.
i have tried the event below but it still prints and the msgbox is not showing
Private Sub Workbook_BeforePrint(Cancel As Boolean)
Cancel = True
MsgBox "CANNOT PRINT, check box 1 is unchecked", vbOKOnly, "Error"
End Sub
any suggestions?
You need to actually test if the checkbox is checked or not. Also, you need to place this code inside the ThisWorkbook module in the VBE.
Change sheet and checkbox name as needed:
Private Sub Workbook_BeforePrint(Cancel As Boolean)
If Sheets("Sheet1").CheckBox1.Value = False Then
Cancel = True
MsgBox "CANNOT PRINT, check box 1 is unchecked", vbOKOnly, "Error"
End If
End Sub

Excel VBA checkbox click and change events the same?

I have 5 checkboxes to set some options for a macro, one of them is a Select/Unselect All checkbox. I want to create something similar to what you have on web-based mailboxes when you select mails to delete or mark as read, etc.
When I check the Select/Unselect All checkbox, I turn the rest of the checkboxes's values to true and viceversa when I uncheck it. That's ok.
The problem comes when I also want to validate that if everything is unchecked and one by one I check the other checkboxes, if in the end I check all, then the Select/Unselect All checkbox turns to checked. And viceversa, meaning that if eveything is checked and then I uncheck one of the four others, then I turn the "All" checkbox to false (unchecked).
But it seems that even when I just set the value, for example Option1Checkbox.value = True, in the SelectAllCheckbox_Click event, it triggers both the Option1Checkbox_Click and Option1Checkbox_Change events.
Shouldn't it just trigger the Change event since I'm not actually clicking that checkbox?
What happens is that I check the SelectAll, so it turns Option1 to checked, but by doing this, Option1 triggers also the click event so it unchecks it which triggers the click event again and then unchecks again the All checkbox which in the end leaves everything unchecked, like at the beginning. Hope this part is clear enough.
How can I avoid this behavior? How can I make sure only the Change event is triggered and not the Click event?
Has anyone ever had such an arrangement of checkboxes and had a similar problem? Or how did you manage to do this without the behavior I'm getting?
The checkboxes are not on a form but simply on a worksheet. They are ActiveX controls. And what I have is nothing complicated:
Private Sub SelectAll_Click()
Option1Checkbox.Value = SelectAll.Value
Option2Checkbox.Value = SelectAll.Value
Option3Checkbox.Value = SelectAll.Value
Option4Checkbox.Value = SelectAll.Value
End Sub
Then the options checkboxes click events look like this:
Private Sub Option1Checkbox_Click()
If Option1Checkbox.Value = True And Option2Checkbox.Value = True And Option3Checkbox.Value = True And Option4Checkbox.Value = True Then
SelectAll.Value = True
Else
SelectAll.Value = False
End If
End Sub
It's quite simple, the biggest problem I see is the calls to the click events when the checkbox is not actually click.
Thanks for your help.
I would define a local variable (one defined outside the subs) and set/check that
Option Explicit
Dim ImChangingStuff As Boolean
Private Sub SelectAll_Click()
ImChangingStuff = True
Option1Checkbox.Value = SelectAll.Value
Option2Checkbox.Value = SelectAll.Value
Option3Checkbox.Value = SelectAll.Value
Option4Checkbox.Value = SelectAll.Value
ImChangingStuff = False
End Sub
then your click routines would look like this:
Private Sub Option1Checkbox_Click()
If ImChangingStuff Then Exit Sub
If Option1Checkbox.Value = True And Option2Checkbox.Value = True And Option3Checkbox.Value = True And Option4Checkbox.Value = True Then
SelectAll.Value = True
Else
SelectAll.Value = False
End If
End Sub
To solve this problem,
in your main userform (in this example it is called Mainform) add this code:
Private Sub Mainform_Initialize()
stateCheckbox = True
End Sub
Create a module to store your global variables (eg. globalVariables), and add the following, which will hold the state of the checkbox:
Public stateCheckbox as Boolean
In the _click event of your checkbox, wrap your code like this:
if stateCheckbox = false then
{ whatever your code is }
Else:
stateCheckbox = false
end if
I have had the same issue as described above. I recognized that each change of a Checkbox, no matter if by click or by changing Checkbox.Value, each time first fires the Checkbox_Change event and then the Checkbox_Click event , provided that any of these events are declared.
I would thus recommend to use only one event for each checkbox, p.e. the Checkbox_Change event, and to check if the checkbox has been changed by click or by declaration at the beginning of the Sub. This can be done by comparing the ActiveControl.Name with the Checkbox.Name:
Private Sub CheckBox1_Change()
On Error Goto Err:
If ActiveControl.Name = CheckBox1.Name Then
On Error Goto 0
'Commands for click
Exit Sub
Else
On Error Goto 0
'Commands for change from within the Userform
Exit Sub
Err:On Error Goto 0
'Commands for change from outside the Userform
End Sub
I would write a sub checking if Option1 to Option4 are all checked in an If statement. In a second if statemement, I'd check to see if they are all false:
Sub Are_O1_to_O4_All_Checked ()
If Option1Checkbox.Value = True And _
Option2Checkbox.Value = True And _
Option3Checkbox.Value = True And _
Option4Checkbox.Value = True Then
Option5Checkbox.Value = True ' select/unselect checkbox
End If
If Option1Checkbox.Value = False And _
Option2Checkbox.Value = False And _
Option3Checkbox.Value = False And _
Option4Checkbox.Value = False Then
Option5Checkbox.Value = False ' select/unselect checkbox
End If
End Sub
Then I would Call this sub at the end of each OnClick event for the check boxes.

Resources