VBA - Split 函式



Split 函式返回一個數組,其中包含根據分隔符拆分的特定數量的值。

語法

Split(expression[,delimiter[,count[,compare]]]) 

引數說明

  • Expression − 一個必需的引數。可以包含帶分隔符的字串的字串表示式。

  • Delimiter − 一個可選引數。用於根據分隔符轉換為陣列的引數。

  • Count − 一個可選引數。要返回的子字串的數量,如果指定為 -1,則返回所有子字串。

  • Compare − 一個可選引數。此引數指定要使用哪個比較方法。

    • 0 = vbBinaryCompare - 執行二進位制比較

    • 1 = vbTextCompare - 執行文字比較

示例

新增一個按鈕並新增以下函式。

Private Sub Constant_demo_Click()
   ' Splitting based on delimiter comma '$'
   Dim a as Variant
   Dim b as Variant
   
   a = Split("Red $ Blue $ Yellow","$")
   b = ubound(a)
   
   For i = 0 to b
      msgbox("The value of array in " & i & " is :"  & a(i))
   Next
End Sub

執行上述函式時,會產生以下輸出。

The value of array in 0 is :Red 
The value of array in 1 is : Blue 
The value of array in 2 is : Yellow
vba_arrays.htm
廣告
© . All rights reserved.