Case select error on string detection?

RayCC

New Member
Joined
Oct 11, 2023
Messages
3
Office Version
  1. 2016
Platform
  1. Windows
just a simple Select case problem,
If I type the "text" or even any text in the Range("B3"), it will fall into "Case >200" statement, that means the answer of C3 is "Excellent"
If I place Case "text" before Case >200, it correctly display TEXT instead of "Excellent"
why any string or text will get into Case >200? they are not a number at all?

VBA Code:
Sub Simple_Case()
    Select Case Range("B3").Value
   
        Case 1 To 200
            Range("C3").Value = "Good"
        Case 0
            Range("C3").Value = ""
        Case Is > 200 'cannot place before text otherwise all text belong in this statement
            Range("C3").Value = "Excellent"
        Case Is = "text"
            Range("C3").Value = "TEXT"
        Case Else
            Range("C3").Value = "Bad"
   
    End Select
End Sub
 
Last edited by a moderator:

Excel Facts

Format cells as currency
Select range and press Ctrl+Shift+4 to format cells as currency. (Shift 4 is the $ sign).
It's the way comparisons are done in VBA. The number (200) is converted to text (so that the types match) and then compared to the entered text.
 
Upvote 0
if its just a case of stopping text


VBA Code:
Sub Simple_Case()
If IsNumeric(Range("B3").Value) Then
Select Case Range("B3").Value

Case 1 To 200
Range("C3").Value = "Good"
Case 0
Range("C3").Value = ""
Case Is > 200 'cannot place before text otherwise all text belong in this statement
Range("C3").Value = "Excellent"
Case Is = "text"
Range("C3").Value = "TEXT"
Case Else
Range("C3").Value = "Bad"

End Select

Else
    MsgBox "Entry is not a number"
End If
End Sub
 
Upvote 0
It's the way comparisons are done in VBA. The number (200) is converted to text (so that the types match) and then compared to the entered text.
got it. thanks a lot. which means I have to make sure the input can only be either all text variables or all numbers variables.
 
Upvote 0
if its just a case of stopping text


VBA Code:
Sub Simple_Case()
If IsNumeric(Range("B3").Value) Then
Select Case Range("B3").Value

Case 1 To 200
Range("C3").Value = "Good"
Case 0
Range("C3").Value = ""
Case Is > 200 'cannot place before text otherwise all text belong in this statement
Range("C3").Value = "Excellent"
Case Is = "text"
Range("C3").Value = "TEXT"
Case Else
Range("C3").Value = "Bad"

End Select

Else
    MsgBox "Entry is not a number"
End If
End Sub
great suggestion... thanks for your help!!!
 
Upvote 0
No problem, could divided in to 2 cases. One for text one for numbers if required.
 
Upvote 0
Cleaned version

VBA Code:
Sub Simple_Case()
If IsNumeric(Range("B3").Value) Then
    
    Select Case Range("B3").Value
        Case 1 To 200
        Range("C3").Value = "Good"
        Case 0
        Range("C3").Value = ""
        Case Is > 200
        Range("C3").Value = "Excellent"
        Case Else
        Range("C3").Value = "Bad"
    End Select
Else
    MsgBox "Entry is not a number"
End If
End Sub
 
Upvote 0
You could simply check for the type in the cell:
VBA Code:
    Select Case TypeName(Range("a1").Value)
    Case "String"
        MsgBox "text!"
    Case "Date"
    Case "Double"
        'Your Select case goes here
    End Select
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,185
Members
453,020
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