Need Help Using Case

My Aswer Is This

Well-known Member
Joined
Jul 5, 2014
Messages
19,512
Office Version
  1. 2021
Platform
  1. 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.

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:

Excel Facts

Format cells as time
Select range and press Ctrl+Shift+2 to format cells as time. (Shift 2 is the @ sign).
Come on now. I help a lot of people here. No one can help me with My Case question. It's just a case if question.

I have never used case if else
 
Upvote 0
Take a look at this and see if it helps. You can nest case statements:

Code:
For Each ctrl In Me.Controls
    Select Case True
        Case TypeOf ctrl Is MSForms.CheckBox, TypeOf ctrl Is MSForms.OptionButton
            Select Case ctrl.Value
                Case True
                    ctrl.BackColor = vbGreen
                Case Else
                    ctrl.BackColor = vbRed
            End Select
        Case TypeOf ctrl Is MSForms.TextBox, TypeOf ctrl Is MSForms.ComboBox
            Select Case ctrl.Value
                Case vbNullString
                    ctrl.BackColor = vbRed
                Case Else
                    ctrl.BackColor = vbGreen
            End Select
    End Select
Next ctrl
 
Upvote 0
Thank you Steve. That worked perfect. Now I understand Case more.
Take a look at this and see if it helps. You can nest case statements:

Code:
For Each ctrl In Me.Controls
    Select Case True
        Case TypeOf ctrl Is MSForms.CheckBox, TypeOf ctrl Is MSForms.OptionButton
            Select Case ctrl.Value
                Case True
                    ctrl.BackColor = vbGreen
                Case Else
                    ctrl.BackColor = vbRed
            End Select
        Case TypeOf ctrl Is MSForms.TextBox, TypeOf ctrl Is MSForms.ComboBox
            Select Case ctrl.Value
                Case vbNullString
                    ctrl.BackColor = vbRed
                Case Else
                    ctrl.BackColor = vbGreen
            End Select
    End Select
Next ctrl
 
Upvote 0

Forum statistics

Threads
1,215,482
Messages
6,125,058
Members
449,206
Latest member
Healthydogs

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top