0

I'm trying to make a select case that identifies if a number is lower than 0, 1 to 100 or greater than 100, the thing is that is just doesn't work. Here's my code:

If IsNumeric(TxtTemp.Text) Then

    Select Case TxtTemp.Text

        Case Is <= 0
            TxtEstado.Text = "Solid"

        Case 1 To 100
            TxtEstado.Text = "Liquid"

        Case Is > 100
            TxtEstado.Text = "Gas"

    End Select

Else

TxtEstado.Text = ""

End If

I know that this is an easy thing to do, the thing is that the select case returns liquid only if the number received is equal to 1. If it is lower or equal to 0 it returns solid, but if it is equal or greater to 2, it returns gas. I don't understand what I'm doing wrong.

  • 2
    Try `Select Case CDbl(TxtTemp.Text)` You need to be comparing a number against those other numbers. – Tim Williams Apr 17 '20 at 23:16
  • Remember this for any programming language: "The String or Character `1` is not the same as the number 1", even though they look the same visually. So you need to convert into the same Data type to compare. – PatricK Apr 18 '20 at 00:10
  • just to add that ` Case Is > 100` could be `Case Else` here – HTH Apr 18 '20 at 06:10
  • Okay, it makes sense, what I ended up doing is storing the 'TxtTemp.Text' in a double variable and it worked, now it makes sense why it worked and your answer is an even better way of doing it. Thank you! – EdgarCantu Apr 18 '20 at 19:22

1 Answers1

0

Maybe it is easier to use a function for this kind of conversion

    Function chText(txt As String) As String

    On Error GoTo EH

        Dim resTxt As String

        If IsNumeric(txt) Then
            Select Case CDbl(txt)
                Case Is <= 0
                    resTxt = "Solid"
                Case 1 To 100
                    resTxt = "Liquid"
                Case Is > 100
                    resTxt = "Gas"
            End Select
        Else
            resTxt = ""
        End If

        chText = resTxt

        Exit Function

    EH:
        chText = "Error"

    End Function

Sub Tester()

 Debug.Print chText("101")
' TxtEstado.Text = chText(TxtTemp.Text)

End Sub
Storax
  • 11,158
  • 3
  • 16
  • 33