The following example demonstrates the correct syntax for procedure
declarations with an array:
Function YourFunctionName (InArray() As String) As Integer
Sub YourSubName (InArray() As String)
The following example demonstrates the correct syntax for procedure calls
with an array:
Result = YourFunctionName(YourArrayName())
YourSubName YourArrayName()
NOTE: When calling procedures in Visual Basic for Applications, you do not have to include the opening and closing parentheses after "YourArrayName" in the above example.
Example
The following example demonstrates a sample user-defined function that
loads an array with string values:
- Create a module and type the following line in the Declarations
section if it is not already there:
- Type the following procedures:
'---------------------------------------------------------------
'The function LoadArray() loads an array called MyArray
'with string values. After loading the array, the function
'calls a procedure that outputs each array element
'to the Immediate window.
'---------------------------------------------------------------
Function LoadArray()
Dim i as Integer
ReDim MyArray(10) As String
For i = 1 to 10
MyArray(i) = "Test Value: " & i
Next i
DisplayArray MyArray()
End Function
'---------------------------------------------------------------
'LoadArray() Sub Procedure
'---------------------------------------------------------------
Sub DisplayArray (InArray() As String)
Dim i as Integer
For i = 1 to UBound(InArray)
Debug.Print InArray(i)
Next i
End Sub
- To test this function, type the following line in the Immediate window, and then press ENTER:
Note that the following list appears the Immediate window:
Test Value: 1
Test Value: 2
Test Value: 3
Test Value: 4
Test Value: 5
Test Value: 6
Test Value: 7
Test Value: 8
Test Value: 9
Test Value: 10