繁体   English   中英

VBA Select案例1到100只取1

[英]VBA Select case 1 to 100 only taking 1

我正在尝试制作一个 select 案例来识别一个数字是小于 0、1 到 100 还是大于 100,事实是那是行不通的。 这是我的代码:

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

我知道这很容易做到,问题是 select 案例只有在收到的数字等于 1 时才会返回液体。如果它小于或等于 0,它会返回固体,但如果它等于或大于2、回气。 我不明白我做错了什么。

也许使用 function 进行这种转换更容易

    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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM