VBA連接操作

下表列出了所有的VBScript語言支持的連接操作。假設變量A=5和變量B=10,則:

操作符

描述

例子

+

相加兩個變量值的數值

A + B = 15

&

連接兩個值

A & B = 510

示例

試試下面的例子就明白了在 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

試試下面的例子就明白了所有VBA中可用的邏輯運算符,創建一個按鈕,並添加以下函數:

Concatenated value:1 is 15

Concatenated value:2 is 510

串聯,也可以用於連接兩個字符串。假設變量A="Microsoft"和變量B =「VBScript」,則:

操作符

描述

例子

+

連接兩個值

A + B = MicrosoftVBScript

&

連接兩個值

A & B = MicrosoftVBScript

示例

試試下面的例子就明白了所有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

將它保存爲 html 和Internet Explorer打開它,那麼上面的腳本會產生以下結果:

Concatenated value:1 is MicrosoftVBScript

Concatenated value:2 is MicrosoftVBScript