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.
 

Some videos you may like

Excel Facts

How to show all formulas in Excel?
Press Ctrl+` to show all formulas. Press it again to toggle back to numbers. The grave accent is often under the tilde on US keyboards.

Norie

Well-known Member
Joined
Apr 28, 2004
Messages
75,811
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
46,077
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
46,077
Office Version
  1. 365
Platform
  1. Windows
You're welcome
 

Watch MrExcel Video

Forum statistics

Threads
1,108,614
Messages
5,523,916
Members
409,542
Latest member
Shezz01

This Week's Hot Topics

Top