Checkbox control in Word UserForm - vba

I have a Word UserForm (Word 2007) with checkboxes and two command controls - Ok and Cancel.
When the form is activated from the macro menu or from an assigned icon, and I click on the checkboxes nothing happens. When I click on the OK button a message appears telling me that I haven't selected anything! When I click on the Cancel button the form unloads.
The checkboxes consist of the Click event which toggles from checked to uncheck. There's also a SelectAll checkbox. When clicked all the other checkboxes are checked or unchecked. When one of the other checkboxes is unchecked the SelectAll checkbox is also unchecked.
When I activate the form from the VBE everything works.
Here's a sample of what I'm talking about:
Sub Loadform()
Load UserForm1
UserForm1.Show
End Sub
Private Sub btnCancel_Click()
Unload Me
End Sub
Private Sub btnOK_Click()
If Me.CheckBox2.Value = True And Me.CheckBox3.Value = True Then
MsgBox "All checkboxes are checked"
ElseIf Me.CheckBox2.Value = True Then
MsgBox Me.CheckBox2.Name & " is checked"
ElseIf Me.CheckBox3.Value = True Then
MsgBox Me.CheckBox3.Name & " is checked"
ElseIf Me.CheckBox2.Value = False And Me.CheckBox3.Value = False Then
MsgBox "You haven't selected any checkboxes."
End If
End Sub
Private Sub CheckBox2_Click()
If Me.CheckBox2.Value = True Then
Me.CheckBox2.Value = False
Me.ckbSelectAll.Value = False
Else
Me.CheckBox2.Value = True
End If
End Sub
Private Sub CheckBox3_Click()
If Me.CheckBox3.Value = True Then
Me.CheckBox3.Value = False
Me.ckbSelectAll.Value = False
Else
Me.CheckBox3.Value = True
End If
End Sub
Private Sub ckbSelectAll_Click()
If Me.ckbSelectAll.Value = True Then
Me.ckbSelectAll.Value = False
Else
Me.ckbSelectAll.Value = True
End If
If ckbSelectAll.Value = False Then
Me.CheckBox2.Value = False
Me.CheckBox3.Value = False
Else
Me.CheckBox2.Value = True
Me.CheckBox3.Value = True
End If
End Sub

In your click event handlers, such as
Private Sub CheckBox3_Click()
...
End Sub
you check to see if the checkbox is checked (i.e. the value is true) and if it is, set the value to false. This means that a checkbox will always be unchecked as soon as it is checked (or always checked as soon as it is unchecked), giving the appearance that checkboxes don't work properly.
Here's an example click event handler to start (I haven't written VBA for a long time, but I think the following is fine. Will test now...)
Private Sub CheckBox2_Click()
' If checkbox2 is checked but checkbox3 is not, '
' uncheck the select all checkbox '
If CheckBox2 And Not CheckBox3Then
ckbSelectAll = False
End If
End Sub
If you need any further help or tips, then please leave a comment

Related

Select All CheckBoxes from single CheckBox in VBA on PowerPoint

I am using PowerPoint to create UI mockups and have enabled the developer tools that create premade checkboxes, etc. I want to have a single checkbox that when selected, selects all the others on the slide.
I have tried the following code, which is accepted syntactically, but does nothing visibly.
this is a modified version from https://www.extendoffice.com/documents/excel/2692-excel-checkbox-select-all.html
CheckBox11 is my "Select All" check box and CheckBox 1-10 need to be selected or deselected based on it being clicked.
Sub SelectAll_Click()
Dim xCheckBox As CheckBox
For Each xCheckBox In Application.ActiveWindow.View.Slide.Checkboxes
If xCheckBox.Name <> Application.ActiveWindow.View.Slide.Checkboxes("Check Box 11").Name Then
xCheckBox.Value = Application.ActiveWindow.View.Slide.Checkboxes("Check Box 11").Value
End If
Next
End Sub
Private Sub CheckBox11_()
SelectAll_Click
End Sub
How can I get this checkbox to check or uncheck the rest on the slide?
I figured this out. Kind of a naive solution but
Private Sub SelectAll()
If CheckBox11.Value = True Then
CheckBox1.Value = True
CheckBox2.Value = True
CheckBox3.Value = True
CheckBox4.Value = True
CheckBox5.Value = True
CheckBox6.Value = True
CheckBox7.Value = True
CheckBox8.Value = True
CheckBox9.Value = True
CheckBox10.Value = True
End If
If CheckBox11.Value = False Then
CheckBox1.Value = False
CheckBox2.Value = False
CheckBox3.Value = False
CheckBox4.Value = False
CheckBox5.Value = False
CheckBox6.Value = False
CheckBox7.Value = False
CheckBox8.Value = False
CheckBox9.Value = False
CheckBox10.Value = False
End If
End Sub
Private Sub CheckBox11_Click()
SelectAll
End Sub

VBA if in text is in textbox then do something

I've written the following code so that if a certain text exists in my listbox and "ok" button is clicked a certain thing is done.
Private Sub CommandButton3_Click()
If (Me.ListBox2.Text) <> ("PA") Then
Call macro1
ElseIf (Me.ListBox2.Text) <> "menu" Then
Sheets("menu").Visible = xlSheetVisible
Worksheets("menu").Activate
Else
MsgBox "Nothing is selected"
End If
End Sub
The problem is that when "ok" is clicked all events are still carried out even if the specified text isn't in the textbox.
You probably want to use = operator, and not <> operator. Also note that ListBox.List(i) is the correct way of getting selected item for single selection mode:
Private Sub CommandButton3_Click()
Dim SelectedItem = ListBox1.List(ListBox1.ListIndex)
If SelectedItem = "PA" Then
Call macro1
ElseIf SelectedItem = "menu" Then
Sheets("menu").Visible = xlSheetVisible
Worksheets("menu").Activate
Else
MsgBox "Nothing is selected"
End If
End Sub
Edit
Following your comment, you can create a function that looks for the existence of that item:
Private Function TextExists(text as String) as Boolean
Dim i as Long
For i = 0 To ListBox1.ListCount - 1
If ListBox1.List(i) = text Then
TextExists = True
Exit Function
End If
Next
TextExists = False
End Function
And then use this function in the main code like this:
Private Sub CommandButton3_Click()
If TextExists("PA") Then
Call macro1
ElseIf TextExists("menu") Then
Sheets("menu").Visible = xlSheetVisible
Worksheets("menu").Activate
Else
MsgBox "Nothing is selected"
End If
End Sub
N.B. I have written this manually here, without an IDE. Please check for indexes and other little things.

Makeshift Cue Banners

I'm trying to create some cue banners for my user form in Word. I got about half way through before I become stuck. I have it where the cue banner will disappear and clear the textbox once it has focus. If the user types their own text, that will be retained as well.
However, if the user doesn't type anything in the textbox once it has been cleared, I want to replace the cue banner along with its attributes (gray text and italicized). I can't seem to get it to work. Here's the code with everything related to this textbox below. The trouble is with the Leave event I think.
Private Sub UserForm_Initialize()
Me.txbShipToName1.Text = "name"
Me.txbShipToName1.Font.Italic = True
Me.txbShipToName1.ForeColor = &H80000006
End Sub
Private Sub txbShipToName1_Enter()
If Me.ActiveControl Is Me.txbShipToName1 And Me.txbShipToName1.Text = "name" Then
txbShipToName1.Font.Italic = False
txbShipToName1.ForeColor = &H80000008
txbShipToName1.Text = ""
End If
End Sub
Private Sub txbShipToName1_Leave()
If Me.ActiveControl Is Not txbShipToName1 And Me.txbShipToName1.Text = "" Then
txbShipToName1.Font.Italic = True
txbShipToName1.ForeColor = &H80000006
txbShipToName1.Text = LCase(txbShipToName1.Text)
txbShipToName1.Text = "name"
End If
End Sub
Private Sub txbShipToName1_Change()
If Me.ActiveControl Is Me.txbShipToName1 And Me.txbShipToName1 <> "name" Then
txbShipToName1.Text = UCase(txbShipToName1.Text)
End If
End Sub
I'm putting this here for anyone else who would like to know the solution. After a few days of messing with it, I finally realize I was over complicating things when I was messing with another piece of code. I eliminated the ActiveControl test because this is essentially already built into the Enter and Exit events. Here's the updated and working code.
Private Sub UserForm_Initialize()
'Populates cue banners
Me.txbShipToName1.Text = "Name"
Me.txbShipToName1.Font.Italic = True
Me.txbShipToName1.ForeColor = &H80000011
End Sub
Private Sub txbShipToName1_Enter()
'Removes cue banner upon entering textbox
If Me.txbShipToName1.Text = "Name" Then
txbShipToName1.Font.Italic = False
txbShipToName1.ForeColor = &H80000012
txbShipToName1.Text = ""
End If
End Sub 'txbShipToName1_Enter()
Private Sub txbShipToName1_Change()
'Converts textbox to uppercase
If Me.txbShipToName1.Text <> "Name" Then
txbShipToName1.Text = UCase(txbShipToName1.Text)
End If
End Sub 'txbShipToName1_Change()
Private Sub txbShipToName1_Exit(ByVal Cancel As MSForms.ReturnBoolean)
'Replaces cue banner upon exiting textbox
If Me.txbShipToName1.Text = "" Then
txbShipToName1.Font.Italic = True
txbShipToName1.ForeColor = &H80000011
txbShipToName1.Text = "Name"
End If
End Sub 'txbShipToName1_Exit(ByVal Cancel As MSForms.ReturnBoolean)

Popup shown twice when checkbox checked

I currently have it set up so that two checkboxes can not be checked at the same time, if they are then it will pop up an error and set the checkbox unchecked again. However currently it is popping up the message twice. How to fix this?
Private Sub CheckBox1_Click()
If CheckBox2.Value = True Then
MsgBox "Must be sent separate."
CheckBox1.Value = False
End If
End Sub
I think you simply need to change it too : the reason is it is running the check when switching checkbox1
Private Sub CheckBox1_Click()
If CheckBox2.Value = True And CheckBox1.Value = True Then
MsgBox "Must be sent separate."
CheckBox1.Value = False
End If
End Sub

Remove shadow from ActiveX Option Button in Excel 2010 VBA

I was trying to emphasis the option button from activeX control when it is selected by user. I decided to show the shadow when it is selected and then hide the shadow when user selects other option button. The first process is working whereas the shadow cannot be removed even though I select other button. My VBA code is shown below:
Private Sub OptionButton1_Click()
OptionButton1.Shadow = False
If OptionButton1.Value = True Then
OptionButton1.Shadow = True
Else
OptionButton1.Shadow = False
End If
End Sub
Can anyone please help me to solve this?
In case of FORMS buttons you can use
Sub RemoveFormsButtonShadows()
'A.Leine 21/10/2015
For Each Button In ActiveSheet.Shapes
Button.Select
Selection.ShapeRange.Shadow.Visible = False
Next Button
End Sub
For that you have to create one sub which needs to be called from all the option buttons that you have. This common sub will simply remove the shadow from all option buttons. Here I am taking the example of 3 option buttons.
Option Explicit
Private Sub OptionButton1_Click()
RemoveShadow
If OptionButton1.Value = True Then _
OptionButton1.Shadow = True
End Sub
Private Sub OptionButton2_Click()
RemoveShadow
If OptionButton2.Value = True Then _
OptionButton2.Shadow = True
End Sub
Private Sub OptionButton3_Click()
RemoveShadow
If OptionButton3.Value = True Then _
OptionButton3.Shadow = True
End Sub
Sub RemoveShadow()
Dim objOpt As OLEObject
With ActiveSheet
For Each objOpt In .OLEObjects
If TypeName(objOpt.Object) = "OptionButton" Then
objOpt.Shadow = False
End If
Next
End With
End Sub