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

Links? Where??
If Excel says you have links but you can't find them, go to Formulas, Name Manager. Look for old links to dead workbooks & delete.
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
 
Upvote 0
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!
 
Upvote 0
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:
Upvote 0
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,
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

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