例 1
' Convert string to boolean
' Display value and data type
Dim nvar As Variant
Dim nstr As String
nstr = Inputbox("Number", "Enter any number", "0")
If Isnumeric(nstr) Then
nvar = Cbool(nstr)
Messagebox nvar,, Typename(nvar)
Else
Messagebox nstr,, "Not a number"
End Sub
例 2
' Convert and display Integer and String values converted to Boolean
dim Int_1 as integer
dim String_1 as string
dim Bool_1, Bool_2
Int_1 = 0
print CBool(Int_1) 'prints FALSE
Bool_1 = CBool(Int_1)
Int_1 = 99
print CBool(Int_1) 'prints TRUE
String_1 = "True"
print CBool(String_1) 'prints TRUE
Bool_2 = CBool(String_1)
String_1 = "No Value"
print CBool(String_1) 'Generates type mismatch error (Error 13)
'String value must be "True" or "False" for
'successful conversion to type Boolean
print DataType(Bool_1) 'prints 11 (Boolean)
print DataType(Bool_2) 'prints 11 (Boolean)