个性化阅读
专注于IT技术分析

VBA压缩运算符

压缩运算符比较两个表达式并返回表示它们之间关系的布尔值。

压缩运算符用于比较值以进行验证。在VBA中, 压缩运算符包括小于(<), 大于(>), 等于(=), 小于等于(<=), 大于等于(> =)和不等于(?)。

让我们通过示例查看所有压缩运算符, 如下所示:

1.等于(=):等于运算符用于检查两个值是否相等, 并返回True或False。它也用作赋值运算符。

例如,

Dim int x As Integer 
Dim int y As Integer 
Dim blnResult As Boolean 
int x = 2
int y = 2
If int x = int y then 
    blnResult = True
Else
    blnResult = False 
End if

在上面的示例中, 我们检查int x等于int y。如果为True, 则布尔blnResult的值为True。否则, 它将为False。

输出:

blnResult = True

2.不等于(≠):不等于运算符用于检查两个值是否不相等, 并返回True或False。

例如,

Dim int x As Integer 
Dim int y As Integer 
Dim blnResult As Boolean 
int x = 2
int y = 3
If int x ≠ int y then 
    blnResult = True
Else
    blnResult = False 
End if

3.大于(>):“大于”运算符用于检查第一个值是否大于第二个值, 并返回True或False。

例如,

Dim int x As Integer 
Dim int y As Integer 
Dim blnResult As Boolean 
int x = 20
int y = 10
If int x > int y then 
    blnResult = True
Else
    blnResult = False 
End if

在上面的示例中, 我们检查int x大于int y。如果为True, 则布尔blnResult的值为True。否则, 它将为False。

输出:

blnResult = True

4.大于等于(> =):大于等于运算符用于检查第一个值是否大于或等于第二个值, 并返回True或False。

例如,

Dim int x As Integer 
Dim int y As Integer 
Dim blnResult As Boolean 
int x = 5
int y = 5
If int x >= int y then 
    blnResult = True
Else
    blnResult = False 
End if

在上面的示例中, 我们检查int x是否大于或等于int y。如果为True, 则布尔blnResult的值为True。否则, 它将为False。

输出:

as both variables are equal to 5 the blnResult returns True.

5.小于(<):小于运算符用于检查第一个值是否小于第二个值, 并返回True或False。

例如,

Dim int x As Integer
Dim int y As Integer
int = 1
int = 2
If int x < int y then
    blnResult = True
  Else
    blnResult = False
End if

在上面的示例中, 我们检查int x小于int y。如果为True, 则布尔blnResult的值为True。否则, 它将为False。

输出:

blnResult = True

6.小于等于(<=):小于等于运算符用于检查第一个值是否小于或等于第二个值, 并返回True或False。

例如,

Dim int x As Integer
Dim int y As Integer
int = 10
int = 10
If int x <= int y then
    blnResult = True
  Else
    blnResult = False
End if

在上面的示例中, 我们检查int x是否小于或等于int y。如果为True, 则布尔blnResult的值为True。否则, 它将为False。

输出:

As both variables are equal to 10, and the blnResult returns True.

赞(0)
未经允许不得转载:srcmini » VBA压缩运算符

相关推荐

评论 抢沙发

评论前必须登录!