Search code examples
vbaselect-case

VBA Select case 1 to 100 only taking 1


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.


Solution

  • 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