My Aswer Is This
Well-known Member
- Joined
- Jul 5, 2014
- Messages
- 19,621
- Office Version
- 2021
- Platform
- Windows
I’m trying to use this script to determine if UserForm controls are true or false.
I wanted to use case statement.
I cannot understand what is wrong with the script where you see the statement this is not working. I’m wanting to know if Textbox or Combobox has values.
I tried using Else statement but that does not work. The object is if the control is empty or false to change the controls backcolor to Red. If control does have values or is true set backcolor of control to Green. I know how to do this with a loop and use if and else but do not know how to use Else using case. I do not use case very often so not sure how to do this. Please don’t say just do not use case and use loops. I’m trying to learn more about case.
I wanted to use case statement.
I cannot understand what is wrong with the script where you see the statement this is not working. I’m wanting to know if Textbox or Combobox has values.
I tried using Else statement but that does not work. The object is if the control is empty or false to change the controls backcolor to Red. If control does have values or is true set backcolor of control to Green. I know how to do this with a loop and use if and else but do not know how to use Else using case. I do not use case very often so not sure how to do this. Please don’t say just do not use case and use loops. I’m trying to learn more about case.
Code:
Private Sub CommandButton2_Click()
Dim ctrl As MSForms.Control
For Each ctrl In Me.Controls
Select Case True
Case TypeOf ctrl Is MSForms.CheckBox
ctrl.Value = False
ctrl.BackColor = vbRed
ctrl.Value = True
ctrl.BackColor = vbGreen
Case TypeOf ctrl Is MSForms.TextBox
ctrl.Value = vbNullString
ctrl.BackColor = vbRed
ctrl.Value = True 'This does not work
ctrl.BackColor = vbGreen
Case TypeOf ctrl Is MSForms.ComboBox
ctrl.Value = vbNullString
ctrl.BackColor = vbRed
ctrl.Value = True 'This does not work
ctrl.BackColor = vbGreen
Case TypeOf ctrl Is MSForms.OptionButton
ctrl.Value = False
ctrl.BackColor = vbRed
ctrl.Value = True
ctrl.BackColor = vbGreen
End Select
Next ctrl
End Sub
Last edited: