VBA基础学习之1.3运算符
程序员文章站
2022-03-16 18:49:16
...
VBA基础学习之运算符
运算符可以用一个简单的表达式定义,例如:4 + 5等于9。这里,4和5称为操作数,+被称为运算符。VBA支持以下类型的运算符 -
- 算术运算符
- 比较运算符
- 逻辑(或关系)运算符
- 连接运算符
算术操作符
以下是VBA支持算术运算符。
假设变量A=5,变量B=10,那么 -
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
比较运算符
VBA支持的比较运算符如下所示。
假设变量A=10,变量B=20,则 -
Private Sub Constant_demo_Click()
Dim a: a = 10
Dim b: b = 20
Dim c
If a = b Then
MsgBox ("Operator Line 1 : True")
Else
MsgBox ("Operator Line 1 : False")
End If
If a<>b Then
MsgBox ("Operator Line 2 : True")
Else
MsgBox ("Operator Line 2 : False")
End If
If a>b Then
MsgBox ("Operator Line 3 : True")
Else
MsgBox ("Operator Line 3 : False")
End If
If a<b Then
MsgBox ("Operator Line 4 : True")
Else
MsgBox ("Operator Line 4 : False")
End If
If a>=b Then
MsgBox ("Operator Line 5 : True")
Else
MsgBox ("Operator Line 5 : False")
End If
If a<=b Then
MsgBox ("Operator Line 6 : True")
Else
MsgBox ("Operator Line 6 : False")
End If
End
逻辑运算符
以下由VBA支持的逻辑运算符。
假设变量A=10,变量B=0,则 -
连接操作符
VBA支持以下连接运算符。
假设变量A=5,变量B=10,则 -
Private Sub Constant_demo_Click()
Dim a as Integer : a = 5
Dim b as Integer : b = 10
Dim c as Integer
c = a + b
msgbox ("Concatenated value:1 is " &c) 'Numeric addition
c = a & b
msgbox ("Concatenated value:2 is " &c) 'Concatenate two numbers
End Sub
Private Sub Constant_demo_Click()
Dim a as String : a = "Microsoft"
Dim b as String : b = "VBScript"
Dim c as String
c = a + b
msgbox("Concatenated value:1 is " &c) 'addition of two Strings
c = a & b
msgbox("Concatenated value:2 is " &c) 'Concatenate two String
End Sub