位置:首頁 > 高級語言 > VBA教學 > VBA算術運算符

VBA算術運算符

下表列出了所有VBScript語言支持的算術運算符。假設變量A=5和變量B=10,則:

運算符 描述 例子
+ 相加兩個操作數 A + B = 15
- 第一操作數減去第二操作數 A - B = -5
* 兩個操作數相乘 A * B = 50
/ 通過分子除以分母 B / A = 2
% 模運算和整數相除後的餘 B MOD 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