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:")

    REM 檢驗句子中是否含有「and」。

    REM 未使用下面的指令行

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

    REM 按如下方式使用 CBool 函式:

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

        MsgBox "The word 》and《 appears in the sentence you entered!"

    EndIf

End Sub