VBA基础学习之1.3运算符

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
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值