Compile error: Case Else outside Select Case

Rufus Clupea

Board Regular
Joined
Feb 11, 2019
Messages
85
I've looked up what the title of this post means, but (pardon the pun) that's not the Case. I must have done something else wrong, either syntactically or logically, but I can't see it.

The following Sub works fine until I try to insert the commented lines.

Code:
Private Sub TextBox1_Change()

    Dim i As Integer

    If Len(TextBox1.Value) = 1 Then Exit Sub
    
    If Val(TextBox1.Value) < SpinButton1.Min Then
        TextBox1.Value = SpinButton1.Min
    End If
    
    If Val(TextBox1.Value) > SpinButton1.Max Then
        TextBox1.Value = SpinButton1.Max
    End If
    
    SpinButton1.Value = TextBox1.Value
    Number1 = Val(TextBox1.Value)

Select Case Number1
    Case Is < 18
        Number1_Label1.Caption = "Minor"
       
    Case Is > 65
        Number1_Label1.Caption = "Senior"

[COLOR=#006400]'        For i = 1 To (Number1 - 65)
'            If ConditionA > Number1 Then Number1 = Number1 + 1:
'            If ConditionB > Number1 Then Number1 = Number1 + 2:
'            If ConditionC > Number1 Then Number1 = Number1 + 3:
'            If ConditionD > Number1 Then Number1 = Number1 + 4
'        Next i
'
'        If ConditionA < 1 Or ConditionB < 1 Or ConditionC < 1 Or ConditionD < Then
'            MsgBox "Text"[/COLOR]


    Case Else
        Number1_Label1.Caption = "Adult"
End Select
    
End Sub

Thanks.
 

Excel Facts

Do you hate GETPIVOTDATA?
Prevent GETPIVOTDATA. Select inside a PivotTable. In the Analyze tab of the ribbon, open the dropown next to Options and turn it off

Norie

Well-known Member
Joined
Apr 28, 2004
Messages
76,343
Office Version
  1. 365
Platform
  1. Windows
You re missing an End If.
Code:
Private Sub TextBox1_Change()
Dim I As Long

    If Len(TextBox1.Value) = 1 Then Exit Sub

    If Val(TextBox1.Value) < SpinButton1.Min Then
        TextBox1.Value = SpinButton1.Min
    End If

    If Val(TextBox1.Value) > SpinButton1.Max Then
        TextBox1.Value = SpinButton1.Max
    End If

    SpinButton1.Value = TextBox1.Value
    Number1 = Val(TextBox1.Value)

    Select Case Number1
        Case Is < 18
            Number1_Label1.Caption = "Minor"

        Case Is > 65
            Number1_Label1.Caption = "Senior"

            For I = 1 To (Number1 - 65)
                If ConditionA > Number1 Then Number1 = Number1 + 1:
                If ConditionB > Number1 Then Number1 = Number1 + 2:
                If ConditionC > Number1 Then Number1 = Number1 + 3:
                If ConditionD > Number1 Then Number1 = Number1 + 4
            Next I

            If ConditionA < 1 Or ConditionB < 1 Or ConditionC < 1 Or ConditionD < 1 Then
                MsgBox "Text"
            End If

        Case Else
            Number1_Label1.Caption = "Adult"
    End Select

End Sub
 

Rufus Clupea

Board Regular
Joined
Feb 11, 2019
Messages
85
Thank you so much--that was it! :)

My last experience with BASIC was in the '80s on a C-64 (No such thing as an End-If). I still get confused when an End-If is/isn't required.

Back to the books... again!
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
73,043
Office Version
  1. 365
Platform
  1. Windows
If you have a one line is like these
Code:
If k = 3 Then k = 23 Else k = 3

or
If k = 3 Then k = 23

or (with line continuations)
If k = 3 Then _
k = 23 Else _
k = 3
You do not need and End If, otherwise you do.
 
Last edited:

Rufus Clupea

Board Regular
Joined
Feb 11, 2019
Messages
85
IC.

I had to go back and check my original, because I thought I had originally written one of those (I did not).

Thank you,
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
73,043
Office Version
  1. 365
Platform
  1. Windows
You're welcome
 
Master Excel Bundle

Excel contains over 450 functions, with more added every year. That’s a huge number, so where should you start? Right here with this bundle.

Forum statistics

Threads
1,163,598
Messages
5,832,646
Members
430,151
Latest member
Kacbear

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
Top