当前位置: 首页 > 文档资料 > VBA 中文教程 >

Show 例子 1

优质
小牛编辑
131浏览
2023-12-01

VBA支持以下算术运算符。

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

操作者描述
+添加两个操作数A + B将给出15
-从第一个中减去第二个操作数A - B将给-5
*将两个操作数相乘A * B将给出50
/用分母除以分子B/A会给2
%模数运算符和整数除法后的余数B%A将给出0
^指数运算符B ^ A将给出100000

例子 (Example)

添加一个按钮并尝试以下示例以了解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