CBool Function

将字符串比较或数字比较的结果转换成布尔表达式,或者将单个数字表达式转换成布尔表达式。

语法:

CBool (Expression1 {= | <> | < | > | <= | >=} Expression2) or CBool (Number)

返回值:

布尔

参数:

Expression1、Expression2:要比较的任意字符串或数字表达式。如果表达式匹配,CBool 函数将返回 True,否则将返回 False

Number:要转换的任意数字表达式。如果表达式等于 0,则返回 False,否则返回 True

下面的示例使用 CBool 函数对 Instr 函数返回的值进行求值。函数将检查用户输入的句子中是否含有 "and"。

错误代码:

5 无效的过程调用

示例:

Sub ExampleCBool

Dim sText As String

    sText = InputBox("Please enter a short sentence:")

    ' 验证句子中是否出现单词“and”。

    ' 未使用下面的命令行

    ' If Instr(Input, "and")<>0 Then...

    ' CBool 函数按如下方式使用:

    If CBool(Instr(sText, "and")) Then

        MsgBox "您输入的句子中出现了 "and"!"

    EndIf

End Sub