📜  VBA-运营商

📅  最后修改于: 2020-11-19 09:10:56             🧑  作者: Mango


可以使用简单表达式-4+来定义运算符。 5等于9。这里,4和5称为操作数,+被称为运算符。 VBA支持以下类型的运算符-

  • 算术运算符
  • 比较运算符
  • 逻辑(或关系)运算符
  • 串联运算符

算术运算符

VBA支持以下算术运算运算符。

假设变量A持有5,变量B持有10,则-

显示范例

Operator Description Example
+ Adds the two operands A + B will give 15
Subtracts the second operand from the first A – B will give -5
* Multiplies both the operands A * B will give 50
/ Divides the numerator by the denominator B / A will give 2
% Modulus operator and the remainder after an integer division B % A will give 0
^ Exponentiation operator B ^ A will give 100000

比较运算符

有通过VBA支持以下运算符。

假设变量A持有10,变量B持有20,则-

显示范例

Operator Description Example
= Checks if the value of the two operands are equal or not. If yes, then the condition is true. (A = B) is False.
<> Checks if the value of the two operands are equal or not. If the values are not equal, then the condition is true. (A <> B) is True.
> Checks if the value of the left operand is greater than the value of the right operand. If yes, then the condition is true. (A > B) is False.
< Checks if the value of the left operand is less than the value of the right operand. If yes, then the condition is true. (A < B) is True.
>= Checks if the value of the left operand is greater than or equal to the value of the right operand. If yes, then the condition is true. (A >= B) is False.
<= Checks if the value of the left operand is less than or equal to the value of the right operand. If yes, then the condition is true. (A <= B) is True.

逻辑运算符

VBA支持以下逻辑运算符。

假设变量A持有10,变量B持有0,则-

显示范例

Operator Description Example
AND Called Logical AND operator. If both the conditions are True, then the Expression is true. a<>0 AND b<>0 is False.
OR Called Logical OR Operator. If any of the two conditions are True, then the condition is true. a<>0 OR b<>0 is true.
NOT Called Logical NOT Operator. Used to reverse the logical state of its operand. If a condition is true, then Logical NOT operator will make false. NOT(a<>0 OR b<>0) is false.
XOR Called Logical Exclusion. It is the combination of NOT and OR Operator. If one, and only one, of the expressions evaluates to be True, the result is True. (a<>0 XOR b<>0) is true.

串联运算符

VBA支持以下并置运算符。

假设变量A持有5,变量B持有10,则-

显示范例

Operator Description Example
+ Adds two Values as Variable. Values are Numeric A + B will give 15
& Concatenates two Values A & B will give 510

假设变量A =“ Microsoft”和变量B =“ VBScript”,然后-

Operator Description Example
+ Concatenates two Values A + B will give MicrosoftVBScript
& Concatenates two Values A & B will give MicrosoftVBScript

–串联运算符可用于数字和字符串。如果变量包含数字值或字符串值,则输出取决于上下文。