VBA连接操作符
VBA⽀持以下连接运算符。
假设变量A=5,变量B=10,则 -
运算符描述⽰例
+将两个值添加为变量,其值是数字  A + B = 15
&连接两个值  A & B = 510
⽰例1
试试下⾯的例⼦来理解VBScript中可⽤的连接运算符 -
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
执⾏上⾯⽰例代码,得到类似下⾯的结果 -
Concatenated value:1is15
Concatenated value:2is510
Shell
假设变量A = "Microsoft",变量B = "VBScript",则 -
运算符描述⽰例
+连接两个值  A + B的结果为MicrosoftVBScrip
&连接两个值  A & B的结果为MicrosoftVBScrip
注 - 连接操作,可⽤于数字和字符串。输出取决于上下⽂,如果变量保存数字值或字符串值。⽰例2
尝试下⾯的⽰例,通过创建⼀个按钮并添加以下函数来了解VBA中可⽤的所有逻辑运算符。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
vba 字符串函数执⾏上⾯⽰例代码,得到类似下⾯的结果 -
Concatenated value:1is MicrosoftVBScript
Concatenated value:2is MicrosoftVBScript

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。