Show/hide control textbox - vba

I am new to Visual Basic and I don't understand what object I need for this task.
I have 5 "control text boxes" that I want to hide show in Powerpoint. I have the following code and receive a 424 error:
Private Sub CommandButton1_Click()
Activesheet.oleobjects("TextBox2").Visible = False
Activesheet.oleobjects("TextBox9").Visible = False
Activesheet.oleobjects("TextBox8").Visible = False
Activesheet.oleobjects("TextBox7").Visible = False
Activesheet.oleobjects("TextBox6").Visible = False
End Sub
Private Sub CommandButton2_Click()
Activesheet.oleobjects("TextBox2").Visible = True
Activesheet.oleobjects("TextBox9").Visible = True
Activesheet.oleobjects("TextBox8").Visible = True
Activesheet.oleobjects("TextBox7").Visible = True
Activesheet.oleobjects("TextBox6").Visible = True
End Sub

PowerPoint doesn't have worksheets, so there's no Activesheet object. That'll trigger an error right off the bat. To simply toggle visibility, you can do this:
Sub ToggleVisibility()
With ActivePresentation.Slides(1)
.Shapes("TextBox1").Visible = Not .Shapes("TextBox1").Visible
.Shapes("TextBox2").Visible = Not .Shapes("TextBox2").Visible
' and so on
End With
End Sub

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

Using VBA code to modify OptionButton.value is activating the control.Click() Sub

I have a userform with 2 OptionButton choices, and I'm modifying the form (hiding labels and controls, and resizing frame) for the default Option (name = BwaIsNew), but then restoring the full userform when Option #2 (name = BwaIsOld) is selected. (see separate question for background).
When Option #2 is selected I'm calling a fresh userform, and coding the change in value. But this coding of the value dlgInformation.BwaIsOld.Value = True then triggers an event (?) that calls the Sub BwaIsOld_Click() code to run. This then sets up a perpetual loop.
What's the best way to solve this?
Problem code (the one looping) is:
Private Sub BwaIsOld_Click()
Unload Me
dlgInformation.BwaIsNew.Value = False
dlgInformation.BwaIsOld.Value = True
dlgInformation.Show
End Sub
Update:
Thanks #Tim & #CommonSense. I'm still not quite there yet. What am I doing wrong? Here is the code
Public EnableEvents As Boolean
Private Sub UserForm_Initialize()
Me.EnableEvents = True
End Sub
Private Sub BwaIsNew_Click()
Call changeform(280)
End Sub
Private Sub BwaIsOld_Click()
Unload Me
Me.EnableEvents = False
dlgInformation.BwaIsNew.Value = False
dlgInformation.BwaIsOld.Value = True
Me.EnableEvents = True
dlgInformation.Show
End Sub
You need to actually use that EnableEvents in the rest of your code.
BTW I would choose a different name from the built-in Application.EnableEvents property just for clarity.
Public EnableEvents As Boolean
Private Sub UserForm_Initialize()
Me.EnableEvents = True
End Sub
Private Sub BwaIsNew_Click()
'don't respond to events triggered by BwaIsOld_Click
If Me.EnableEvents Then
Call changeform(280)
End If
End Sub
Private Sub BwaIsOld_Click()
Unload Me '<< why do this here?
Me.EnableEvents = False
dlgInformation.BwaIsNew.Value = False
dlgInformation.BwaIsOld.Value = True
Me.EnableEvents = True
dlgInformation.Show
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

excel vba userform enableevents

I'm having a problem with Excel VBA UserForm Events in Office Excel 2013 as follows
Simple userform with three check boxes (CB1,2,3) and two buttons Cancel and OK
When checking CB1 set CB3 = false
When checking CB2 set CB3 = false
When checking CB3 set CB1 = false and CB2 = false
I have read and understood http://www.cpearson.com/excel/SuppressChangeInForms.htm regarding the suppression of UserForm Events and to part it works...
In the list above 2. and 3. above work correctly in code (shown below) and no-events are fired for CB3. However when I do 4. Check CB3 - it fires events for CB1 and CB2, even though I have set it to not fire events.
Any help gratefully received,
Best regards
Seán
Code:
Public EnableEvents As Boolean
Private Sub UserForm_Initialize()
Me.EnableEvents = True
End Sub
Private Sub vboInputsSelected_Click()
Me.EnableEvents = False
vboPracticesSelected.value = False 'this line does NOT fire an event
Me.EnableEvents = True
End Sub
Private Sub vboOutputsSelected_Click()
Me.EnableEvents = False
vboPracticesSelected.value = False 'this line does NOT fire an event
Me.EnableEvents = True
End Sub
Private Sub vboPracticesSelected_Click()
Me.EnableEvents = False
vboInputsSelected.value = False 'this line DOES fire an event
vboOutputsSelected.value = False 'this line DOES fire an event
Me.EnableEvents = True
End Sub
This works well for me. The If bails out when an event is in progress. Realize that the EnableEvents variable does nothing on its own to prevent events. It is only a boolean you created. You need to check it, before allowing an event to occur, for it to do anything.
Public EnableEvents As Boolean
Private Sub vboInputsSelected_Click()
If Not EnableEvents Then Exit Sub
Me.EnableEvents = False
vboPracticesSelected.Value = False
Me.EnableEvents = True
End Sub
Private Sub vboOutputsSelected_Click()
If Not EnableEvents Then Exit Sub
Me.EnableEvents = False
vboPracticesSelected.Value = False
Me.EnableEvents = True
End Sub
Private Sub vboPracticesSelected_Click()
If Not EnableEvents Then Exit Sub
Me.EnableEvents = False
vboInputsSelected.Value = False
vboOutputsSelected.Value = False
Me.EnableEvents = True
End Sub
According to this reference:
it's a better practice to work with a new instance of the class
Below trying to adapt the code:
'http://www.cpearson.com/excel/SuppressChangeInForms.htm
'https://riptutorial.com/vba/example/19036/best-practices
Private Type TView
IsCancelled As Boolean
EnableEvents As Boolean
End Type
Private this As TView
Public Property Get IsCancelled() As Boolean
IsCancelled = this.IsCancelled
End Property
Public Property Get EnableEvents() As Boolean
EnableEvents = this.EnableEvents
End Property
Private Sub UserForm_Initialize()
'...
this.EnableEvents = True
End Sub
Private Sub TextBox1_BeforeUpdate(ByVal Cancel As MSForms.ReturnBoolean)
On Error GoTo ExceptionHandling
this.EnableEvents = False
'some code that would cause an event to run
CleanUp:
On Error Resume Next
this.EnableEvents = True
Exit Sub
ExceptionHandling:
MsgBox "Error: " & Err.description & vbLf & Err.Number
Resume CleanUp
Resume 'for debugging
End Sub
Private Sub TextBox2_BeforeUpdate(ByVal Cancel As MSForms.ReturnBoolean)
'check example
If this.EnableEvents = False Then Cancel = True
'some code to run
End Sub
Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
If CloseMode = VbQueryClose.vbFormControlMenu Then
Cancel = True
this.IsCancelled = True
Me.Hide
End If
End Sub
See Also
VBA UserForm – A Guide for Everyone, Paul Kelly

Checkbox control in Word UserForm

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