VBA advanced array functions in Excel
Arrays have some predefined functions which are very much useful while working with.
The most often used are as detailed below
a) Split()
This method is mostly used to split a string into array of strings based on a delimiter
This method always returns an array of values and should be stored in a array variable
The following example splits a string into array of strings with "@" as Delimiter
- Sub Array_demo()
- Sub Array_Functions()
- Dim str As String
- Dim arr_str() As String
- str = "Welcome @ to @ VBA"
- arr_str = Split(str, "@")
- End Sub
The following illustrates with output
The output is as shown below
b) Lbound and Ubound
As the name indicates, the Lbound method returns an integer that specifies the lowest bound or the starting index of the Array
Similarly, The Ubound method returns the upper bound of the array
Thses methods are useful when working with Arrays of unknown size resulting from Split()
The following example illustrates this.
- Sub Array_Functions()
- Dim str As String
- Dim arr_str() As String
- str = "Welcome @ to @ VBA"
- arr_str = Split(str, "@")
- MsgBox "Starting Index : " & LBound(arr_str) & vbNewLine & _
- Upper bound : & UBound(arr_str)
- End Sub
The Output shows the Lower and Upper Bound and thereby, the size of the array