- VBA 教程
- VBA - 首页
- VBA - 概述
- VBA - Excel 宏
- VBA - Excel 术语
- VBA - 宏注释
- VBA - 消息框
- VBA - 输入框
- VBA - 变量
- VBA - 常量
- VBA - 运算符
- VBA - 决策
- VBA - 循环
- VBA - 字符串
- VBA - 日期和时间
- VBA - 数组
- VBA - 函数
- VBA - 子过程
- VBA - 事件
- VBA - 错误处理
- VBA - Excel 对象
- VBA - 文本文件
- VBA - 图表编程
- VBA - 用户窗体
- VBA 有用资源
- VBA - 快速指南
- VBA - 有用资源
- VBA - 讨论
VBA - 比较运算符
VBA 支持以下比较运算符。
假设变量 A 的值为 10,变量 B 的值为 20,则 -
| 运算符 | 描述 | 示例 |
|---|---|---|
| = | 检查两个操作数的值是否相等。如果相等,则条件为真。 | (A = B) 为假。 |
| <> | 检查两个操作数的值是否相等。如果值不相等,则条件为真。 | (A <> B) 为真。 |
| > | 检查左操作数的值是否大于右操作数的值。如果大于,则条件为真。 | (A > B) 为假。 |
| < | 检查左操作数的值是否小于右操作数的值。如果小于,则条件为真。 | (A < B) 为真。 |
| >= | 检查左操作数的值是否大于或等于右操作数的值。如果大于或等于,则条件为真。 | (A >= B) 为假。 |
| <= | 检查左操作数的值是否小于或等于右操作数的值。如果小于或等于,则条件为真。 | (A <= B) 为真。 |
示例
尝试以下示例以了解 VBA 中所有可用的比较运算符。
Private Sub Constant_demo_Click()
Dim a: a = 10
Dim b: b = 20
Dim c
If a = b Then
MsgBox ("Operator Line 1 : True")
Else
MsgBox ("Operator Line 1 : False")
End If
If a<>b Then
MsgBox ("Operator Line 2 : True")
Else
MsgBox ("Operator Line 2 : False")
End If
If a>b Then
MsgBox ("Operator Line 3 : True")
Else
MsgBox ("Operator Line 3 : False")
End If
If a<b Then
MsgBox ("Operator Line 4 : True")
Else
MsgBox ("Operator Line 4 : False")
End If
If a>=b Then
MsgBox ("Operator Line 5 : True")
Else
MsgBox ("Operator Line 5 : False")
End If
If a<=b Then
MsgBox ("Operator Line 6 : True")
Else
MsgBox ("Operator Line 6 : False")
End If
End Sub
执行上述脚本时,将产生以下结果。
Operator Line 1 : False Operator Line 2 : True Operator Line 3 : False Operator Line 4 : True Operator Line 5 : False Operator Line 6 : True
vba_operators.htm
广告
