The LBound and UBound Functions

The LBound and UBound functions return whole numbers that indicate the lower bound and upper bound indices of an array.

©Hands-On 7-10: Finding the Lower and Upper Bounds of an Array

1. In a new module, enter the code of the FunCities2 procedure shown below.

2. Run the FunCities2 procedure.

Sub FunCities2()

' declare the array

Dim cities(1 To 5) As String

' assign the values to array elements cities(1) = "Las Vegas" cities(2) = "Orlando" cities(3) = "Atlantic City" cities(4) = "New York" cities(5) = "San Francisco"

' display the list of cities

MsgBox cities(1) & Chr(13) & cities(2) & Chr(13) _ & cities(3) & Chr(13) & cities(4) & Chr(13) _ & cities(5)

' display the array bounds

MsgBox "The lower bound: " & LBound(cities) & Chr(13) _ & "The upper bound: " & UBound(cities)

End Sub

When determining the lower and upper bound indices of a two-dimensional array, you must specify the dimension number: 1 for the first dimension and 2 for the second dimension.

To determine the upper and lower indices in a two-dimensional array, you may want to add the following statements at the end of the Exchange procedure that was prepared earlier in this chapter (add these lines just before the End Sub keywords):

MsgBox "The lower bound (first dimension) is " _

& LBound(Ex, 1) & "." MsgBox "The upper bound(first dimension) is " _

& UBound(Ex, 1) & "." MsgBox "The lower bound (second dimension) is " _

& LBound(Ex, 2) & "." MsgBox "The upper bound(second dimension) is " _ & UBound(Ex, 2) & "."

Part I

0 0

Post a comment