以下是VBA支持算术运算符。
假设变量A=5
,变量B=10
,那么 -
运算符 | 描述 | 示例 |
---|---|---|
+ |
两个操作数相加 | A + B = 15 |
- |
两个操作数相减 | A - B = -5 |
* |
两个操作数相乘 | A * B = 50 |
/ |
两个操作数相除 | B / A = 2 |
% |
模运算符,整数除法后的余数 | B % A = 0 |
^ |
指数运算符 | B ^ A = 100000 |
示例
添加一个按钮并尝试下面的示例来了解如何在VBA中使用算术运算符。
Private Sub Constant_demo_Click()
Dim a As Integer
a = 5
Dim b As Integer
b = 10
Dim c As Double
c = a + b
MsgBox ("Addition Result is " & c)
c = a - b
MsgBox ("Subtraction Result is " & c)
c = a * b
MsgBox ("Multiplication Result is " & c)
c = b / a
MsgBox ("Division Result is " & c)
c = b Mod a
MsgBox ("Modulus Result is " & c)
c = b ^ a
MsgBox ("Exponentiation Result is " & c)
End Sub
当点击按钮或执行上面的脚本时,会产生类似下面的结果。
Addition Result is 15
Subtraction Result is -5
Multiplication Result is 50
Division Result is 2
Modulus Result is 0
Exponentiation Result is 100000