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.