Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
2 views

VBA array method

The document provides an overview of various array methods in VBScript, including LBound, UBound, Split, Join, Filter, IsArray, and Erase functions. Each function is described with its syntax, parameters, and examples demonstrating its usage. These methods help developers effectively manage and manipulate arrays in their VBScript code.

Uploaded by

yogitas804
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

VBA array method

The document provides an overview of various array methods in VBScript, including LBound, UBound, Split, Join, Filter, IsArray, and Erase functions. Each function is described with its syntax, parameters, and examples demonstrating its usage. These methods help developers effectively manage and manipulate arrays in their VBScript code.

Uploaded by

yogitas804
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

VBA - Arrays

Array Methods

There are various inbuilt functions within VBScript which help the developers to handle
arrays effectively. All the methods that are used in conjunction with arrays are listed
below. Please click on the method name to know about it in detail.

Sr.N
Function & Description
o.

LBound
1
A Function, which returns an integer that corresponds to the smallest subscript of the given arrays.

UBound
2
A Function, which returns an integer that corresponds to the largest subscript of the given arrays.

Split
3
A Function, which returns an array that contains a specified number of values. Split based on a delimiter.

Join
4
A Function, which returns a string that contains a specified number of substrings in an array. This is an
exact opposite function of Split Method.

Filter
5
A Function, which returns a zero based array that contains a subset of a string array based on a specific
filter criteria.

IsArray
6
A Function, which returns a boolean value that indicates whether or not the input variable is an array.

Erase
7
A Function, which recovers the allocated memory for the array variables.
VBA - LBound Function
The LBound Function returns the smallest subscript of the specified array. Hence, LBound
of an array is ZERO.

Syntax
LBound(ArrayName[,dimension])
Parameter Description
 ArrayName − A required parameter. This parameter corresponds to the name of
the array.
 Dimension − An optional parameter. This takes an integer value that
corresponds to the dimension of the array. If it is '1', then it returns the lower
bound of the first dimension; if it is '2', then it returns the lower bound of the
second dimension and so on.
Example

Add a button and add the following function.

Private Sub Constant_demo_Click()


Dim arr(5) as Variant
arr(0) = "1" 'Number as String
arr(1) = "VBScript 'String
arr(2) = 100 'Number
arr(3) = 2.45 'Decimal Number
arr(4) = #10/07/2013# 'Date
arr(5) = #12.45 PM# 'Time
msgbox("The smallest Subscript value of the given array is : " &
LBound(arr))

' For MultiDimension Arrays :


Dim arr2(3,2) as Variant
msgbox("The smallest Subscript of the first dimension of arr2 is : " &
LBound(arr2,1))
msgbox("The smallest Subscript of the Second dimension of arr2 is : " &
LBound(arr2,2))
End Sub
VBA - UBound Function
The UBound Function returns the largest subscript of the specified array. Hence, this
value corresponds to the size of the array.

Syntax
UBound(ArrayName[,dimension])
Parameter Description
 ArrayName − A required parameter. This parameter corresponds to the name of
the array.
 Dimension − An optional parameter. This takes an integer value that
corresponds to the dimension of the array. If it is '1', then it returns the lower
bound of the first dimension; if it is '2', then it returns the lower bound of the
second dimension, and so on.
Example

Add a button and add the following function.

Private Sub Constant_demo_Click()


Dim arr(5) as Variant
arr(0) = "1" 'Number as String
arr(1) = "VBScript 'String
arr(2) = 100 'Number
arr(3) = 2.45 'Decimal Number
arr(4) = #10/07/2013# 'Date
arr(5) = #12.45 PM# 'Time
msgbox("The smallest Subscript value of the given array is : " &
UBound(arr))

' For MultiDimension Arrays :


Dim arr2(3,2) as Variant
msgbox("The smallest Subscript of the first dimension of arr2 is : " &
UBound(arr2,1))
msgbox("The smallest Subscript of the Second dimension of arr2 is : " &
UBound(arr2,2))
End Sub
VBA - Split Function
A Split Function returns an array that contains a specific number of values split based on
a delimiter.

Syntax
Split(expression[,delimiter[,count[,compare]]])
Parameter Description
 Expression − A required parameter. The string expression that can contain
strings with delimiters.
 Delimiter − An optional parameter. The parameter, which is used to convert into
arrays based on a delimiter.
 Count − An optional parameter. The number of substrings to be returned, and if
specified as -1, then all the substrings are returned.
 Compare − An optional parameter. This parameter specifies which comparison
method is to be used.
o 0 = vbBinaryCompare - Performs a binary comparison
o 1 = vbTextCompare - Performs a textual comparison
Example

Add a button and add the following function.

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
VBA - Join Function
A Function, which returns a string that contains a specified number of substrings in an
array. This is an exact opposite function of Split Method.

Syntax
Join(List[,delimiter])
Parameter Description
 List − A required parameter. An array that contains the substrings that are to be
joined.
 Delimiter − An optional parameter. The character, which used as a delimiter
while returning the string. The default delimiter is Space.
Example

Add a button and add the following function.

Private Sub Constant_demo_Click()


' Join using spaces
a = array("Red","Blue","Yellow")
b = join(a)
msgbox("The value of b " & " is :" & b)

' Join using $


b = join(a,"$")
msgbox("The Join result after using delimiter is : " & b)
End Sub
VBA - Filter Function
A Filter Function, which returns a zero-based array that contains a subset of a string
array based on a specific filter criteria.

Syntax
Filter(inputstrings,value[,include[,compare]])
Parameter Description
 Inputstrings − A required parameter. This parameter corresponds to the array
of strings to be searched.
 Value − A required parameter. This parameter corresponds to the string to
search for against the inputstrings parameter.
 Include − An optional parameter. This is a Boolean value, which indicates
whether or not to return the substrings that include or exclude.
 Compare − An optional parameter. This parameter describes which string
comparison method is to be used.
o 0 = vbBinaryCompare - Performs a binary comparison
o 1 = vbTextCompare - Performs a textual comparison
Example

Add a button and add the following function.

Private Sub Constant_demo_Click()


Dim a,b,c,d as Variant
a = array("Red","Blue","Yellow")
b = Filter(a,"B")
c = Filter(a,"e")
d = Filter(a,"Y")

For each x in b
msgbox("The Filter result 1: " & x)
Next

For each y in c
msgbox("The Filter result 2: " & y)
Next

For each z in d
msgbox("The Filter result 3: " & z)
Next
End Sub
VBA - IsArray Function
The IsArray Function returns a boolean value that indicates whether or NOT the specified
input variable is an array variable.

Syntax
IsArray(variablename)
Example

Add a button and add the following function.

Private Sub Constant_demo_Click()


Dim a,b as Variant
a = array("Red","Blue","Yellow")
b = "12345"

msgbox("The IsArray result 1 : " & IsArray(a))


msgbox("The IsArray result 2 : " & IsArray(b))
End Sub
VBA - Erase Function
The Erase Function is used to reset the values of fixed size arrays and free the memory
of the dynamic arrays. It behaves depending upon the type of the arrays.

Syntax
Erase ArrayName
 Fixed numeric array, each element in an array is reset to Zero.
 Fixed string array, each element in an array is reset to Zero length " ".
 Array of objects, each element in an array is reset to special value Nothing.
Example

Add a button and add the following function.

Private Sub Constant_demo_Click()


Dim NumArray(3)
NumArray(0) = "VBScript"
NumArray(1) = 1.05
NumArray(2) = 25
NumArray(3) = #23/04/2013#

Dim DynamicArray()
ReDim DynamicArray(9) ' Allocate storage space.

Erase NumArray ' Each element is reinitialized.


Erase DynamicArray ' Free memory used by array.

' All values would be erased.


msgbox("The value at Zeroth index of NumArray is " & NumArray(0))
msgbox("The value at First index of NumArray is " & NumArray(1))
msgbox("The value at Second index of NumArray is " & NumArray(2))
msgbox("The value at Third index of NumArray is " & NumArray(3))
End Sub

You might also like