VB6 UBound Function
The UBound function returns a Long containing the largest available subscript for the indicated dimension of an array.
Syntax
UBound(arrayname[, dimension])
Parameters
arrayname: Required. Name of the array variable. Follows standard Visual Basic naming conventions.dimension: Optional. Variant (Long). Specifies which dimension's upper bound is returned. Use 1 for the first dimension, 2 for the second, and so on. Ifdimensionis omitted, 1 is assumed.
Returns
Returns a Long containing the largest available subscript for the specified dimension of the array.
Remarks
The UBound function is used to determine the upper limit of an array dimension:
- Dimension parameter: If omitted, defaults to 1 (first dimension)
- Multi-dimensional arrays: Use
dimensionparameter to specify which dimension - Zero-based arrays:
UBoundreturns the upper index regardless of lower bound - Paired with
LBound: UseLBoundto get the lower bound - Array size calculation: Size =
UBound - LBound + 1 - Dynamic arrays: Returns current upper bound (changes with
ReDim) - Fixed arrays: Returns the declared upper bound
- Error on uninitialized: Error 9 (Subscript out of range) if array not initialized
ParamArray: Works withParamArrayarguments to find number of elements
Common Array Declarations
Dim arr(5) ' LBound = 0, UBound = 5 (6 elements)
Dim arr(1 To 5) ' LBound = 1, UBound = 5 (5 elements)
Dim arr(10 To 20) ' LBound = 10, UBound = 20 (11 elements)
Dim arr(5, 3) ' First: 0-5, Second: 0-3
Dim arr(1 To 5, 1 To 3) ' First: 1-5, Second: 1-3
Option Base Impact
The Option Base statement affects default lower bounds:
- Option Base 0: Default lower bound is 0 (default)
- Option Base 1: Default lower bound is 1
- Explicit bounds (e.g., 1 To 5) override Option Base
Dynamic Arrays
For dynamic arrays:
- Before ReDim: Error 9 if accessed
- After ReDim: Returns current upper bound
- ReDim Preserve: Can change upper bound while preserving data
- Erase: Makes array uninitialized again
Typical Uses
- Loop Bounds: Iterate through all array elements
- Array Size: Calculate the number of elements in an array
- Validation: Check if an index is within valid range
- Dynamic Resizing: Determine current size before
ReDim ParamArray: Count variable number of arguments- Array Copying: Determine target array size
- Search Operations: Set loop limits for array searches
- Multi-dimensional: Navigate complex array structures
Basic Examples
Example 1: Simple Array Iteration
Dim values(10) As Integer
Dim i As Integer
For i = LBound(values) To UBound(values)
values(i) = i * 2
Next i
Example 2: Calculate Array Size
Function GetArraySize(arr() As Variant) As Long
GetArraySize = UBound(arr) - LBound(arr) + 1
End Function
' Usage:
Dim myArray(5 To 15) As String
Debug.Print GetArraySize(myArray) ' Prints: 11
Example 3: Multi-Dimensional Array
Sub ProcessMatrix()
Dim matrix(1 To 3, 1 To 4) As Double
Dim row As Integer
Dim col As Integer
For row = LBound(matrix, 1) To UBound(matrix, 1)
For col = LBound(matrix, 2) To UBound(matrix, 2)
matrix(row, col) = row * col
Next col
Next row
End Sub
Example 4: ParamArray with UBound
Function Sum(ParamArray values() As Variant) As Double
Dim i As Integer
Dim total As Double
total = 0
For i = LBound(values) To UBound(values)
total = total + values(i)
Next i
Sum = total
End Function
' Usage: result = Sum(1, 2, 3, 4, 5)
Common Patterns
Pattern 1: Safe Array Iteration
Sub IterateArray(arr() As Variant)
Dim i As Long
For i = LBound(arr) To UBound(arr)
Debug.Print arr(i)
Next i
End Sub
Pattern 2: Check If Array Is Empty
Function IsArrayEmpty(arr() As Variant) As Boolean
On Error Resume Next
IsArrayEmpty = (UBound(arr) < LBound(arr))
If Err.Number <> 0 Then IsArrayEmpty = True
End Function
Pattern 3: Resize Array with Data Preservation
Sub AddArrayElement(arr() As Variant, newValue As Variant)
Dim newSize As Long
On Error Resume Next
newSize = UBound(arr) + 1
If Err.Number <> 0 Then
' Array not initialized
ReDim arr(0 To 0)
newSize = 0
Else
ReDim Preserve arr(LBound(arr) To newSize)
End If
arr(newSize) = newValue
End Sub
Pattern 4: Count Elements in ParamArray
Function CountArgs(ParamArray args() As Variant) As Long
On Error Resume Next
CountArgs = UBound(args) - LBound(args) + 1
If Err.Number <> 0 Then CountArgs = 0
End Function
Pattern 5: Validate Array Index
Function IsValidIndex(arr() As Variant, index As Long) As Boolean
On Error Resume Next
IsValidIndex = (index >= LBound(arr) And index <= UBound(arr))
If Err.Number <> 0 Then IsValidIndex = False
End Function
Pattern 6: Copy Array
Function CopyArray(source() As Variant) As Variant()
Dim dest() As Variant
Dim i As Long
ReDim dest(LBound(source) To UBound(source))
For i = LBound(source) To UBound(source)
dest(i) = source(i)
Next i
CopyArray = dest
End Function
Pattern 7: Reverse Array
Sub ReverseArray(arr() As Variant)
Dim i As Long
Dim j As Long
Dim temp As Variant
i = LBound(arr)
j = UBound(arr)
While i < j
temp = arr(i)
arr(i) = arr(j)
arr(j) = temp
i = i + 1
j = j - 1
Wend
End Sub
Pattern 8: Find Last Element
Function GetLastElement(arr() As Variant) As Variant
GetLastElement = arr(UBound(arr))
End Function
Pattern 9: Remove Last Element
Sub RemoveLastElement(arr() As Variant)
Dim newUpper As Long
newUpper = UBound(arr) - 1
If newUpper >= LBound(arr) Then
ReDim Preserve arr(LBound(arr) To newUpper)
End If
End Sub
Pattern 10: Multi-Dimensional Size
Function GetArrayDimensions(arr As Variant) As Integer
Dim dimension As Integer
On Error Resume Next
dimension = 1
Do While Err.Number = 0
Dim test As Long
test = UBound(arr, dimension)
dimension = dimension + 1
Loop
GetArrayDimensions = dimension - 1
End Function
Advanced Usage
Example 1: Dynamic Array Manager Class
' Class: DynamicArrayManager
' Manages a dynamic array with automatic resizing
Option Explicit
Private m_Data() As Variant
Private m_Initialized As Boolean
Public Sub Initialize(Optional initialSize As Long = 10)
ReDim m_Data(0 To initialSize - 1)
m_Initialized = True
End Sub
Public Sub Add(value As Variant)
Dim newIndex As Long
If Not m_Initialized Then
Initialize
newIndex = 0
Else
newIndex = UBound(m_Data) + 1
ReDim Preserve m_Data(0 To newIndex)
End If
m_Data(newIndex) = value
End Sub
Public Function GetItem(index As Long) As Variant
If index < LBound(m_Data) Or index > UBound(m_Data) Then
Err.Raise 9, , "Index out of range"
End If
If IsObject(m_Data(index)) Then
Set GetItem = m_Data(index)
Else
GetItem = m_Data(index)
End If
End Function
Public Sub SetItem(index As Long, value As Variant)
If index < LBound(m_Data) Or index > UBound(m_Data) Then
Err.Raise 9, , "Index out of range"
End If
m_Data(index) = value
End Sub
Public Function Count() As Long
If Not m_Initialized Then
Count = 0
Else
Count = UBound(m_Data) - LBound(m_Data) + 1
End If
End Function
Public Sub Clear()
If m_Initialized Then
Erase m_Data
m_Initialized = False
End If
End Sub
Public Function ToArray() As Variant()
ToArray = m_Data
End Function
Example 2: Array Utilities Module
' Module: ArrayUtilities
' Comprehensive array manipulation utilities
Option Explicit
Public Function ArraySize(arr As Variant) As Long
On Error Resume Next
ArraySize = UBound(arr) - LBound(arr) + 1
If Err.Number <> 0 Then ArraySize = 0
End Function
Public Function ArrayContains(arr() As Variant, value As Variant) As Boolean
Dim i As Long
ArrayContains = False
For i = LBound(arr) To UBound(arr)
If arr(i) = value Then
ArrayContains = True
Exit Function
End If
Next i
End Function
Public Function ArrayIndexOf(arr() As Variant, value As Variant) As Long
Dim i As Long
ArrayIndexOf = -1
For i = LBound(arr) To UBound(arr)
If arr(i) = value Then
ArrayIndexOf = i
Exit Function
End If
Next i
End Function
Public Sub ArraySort(arr() As Variant)
Dim i As Long
Dim j As Long
Dim temp As Variant
For i = LBound(arr) To UBound(arr) - 1
For j = i + 1 To UBound(arr)
If arr(i) > arr(j) Then
temp = arr(i)
arr(i) = arr(j)
arr(j) = temp
End If
Next j
Next i
End Sub
Public Function ArrayFilter(arr() As Variant, filterValue As Variant) As Variant()
Dim result() As Variant
Dim i As Long
Dim count As Long
count = 0
For i = LBound(arr) To UBound(arr)
If arr(i) <> filterValue Then
ReDim Preserve result(0 To count)
result(count) = arr(i)
count = count + 1
End If
Next i
ArrayFilter = result
End Function
Public Function ArraySlice(arr() As Variant, startIndex As Long, _
endIndex As Long) As Variant()
Dim result() As Variant
Dim i As Long
Dim idx As Long
ReDim result(0 To endIndex - startIndex)
idx = 0
For i = startIndex To endIndex
result(idx) = arr(i)
idx = idx + 1
Next i
ArraySlice = result
End Function
Example 3: Matrix Operations Class
' Class: MatrixOperations
' Performs operations on 2D arrays
Option Explicit
Public Function GetRowCount(matrix As Variant) As Long
On Error Resume Next
GetRowCount = UBound(matrix, 1) - LBound(matrix, 1) + 1
If Err.Number <> 0 Then GetRowCount = 0
End Function
Public Function GetColumnCount(matrix As Variant) As Long
On Error Resume Next
GetColumnCount = UBound(matrix, 2) - LBound(matrix, 2) + 1
If Err.Number <> 0 Then GetColumnCount = 0
End Function
Public Function GetRow(matrix As Variant, rowIndex As Long) As Variant()
Dim result() As Variant
Dim col As Long
Dim idx As Long
ReDim result(LBound(matrix, 2) To UBound(matrix, 2))
For col = LBound(matrix, 2) To UBound(matrix, 2)
result(col) = matrix(rowIndex, col)
Next col
GetRow = result
End Function
Public Function GetColumn(matrix As Variant, colIndex As Long) As Variant()
Dim result() As Variant
Dim row As Long
ReDim result(LBound(matrix, 1) To UBound(matrix, 1))
For row = LBound(matrix, 1) To UBound(matrix, 1)
result(row) = matrix(row, colIndex)
Next row
GetColumn = result
End Function
Public Function TransposeMatrix(matrix As Variant) As Variant
Dim result() As Variant
Dim row As Long
Dim col As Long
ReDim result(LBound(matrix, 2) To UBound(matrix, 2), _
LBound(matrix, 1) To UBound(matrix, 1))
For row = LBound(matrix, 1) To UBound(matrix, 1)
For col = LBound(matrix, 2) To UBound(matrix, 2)
result(col, row) = matrix(row, col)
Next col
Next row
TransposeMatrix = result
End Function
Example 4: Collection to Array Converter
' Module: CollectionConverter
' Converts between Collections and Arrays
Option Explicit
Public Function CollectionToArray(col As Collection) As Variant()
Dim result() As Variant
Dim i As Long
If col.Count = 0 Then
CollectionToArray = Array()
Exit Function
End If
ReDim result(1 To col.Count)
For i = 1 To col.Count
If IsObject(col(i)) Then
Set result(i) = col(i)
Else
result(i) = col(i)
End If
Next i
CollectionToArray = result
End Function
Public Function ArrayToCollection(arr() As Variant) As Collection
Dim result As New Collection
Dim i As Long
For i = LBound(arr) To UBound(arr)
result.Add arr(i)
Next i
Set ArrayToCollection = result
End Function
Public Function MergeArrays(ParamArray arrays() As Variant) As Variant()
Dim result() As Variant
Dim totalSize As Long
Dim currentIndex As Long
Dim i As Long
Dim j As Long
Dim arr As Variant
' Calculate total size
totalSize = 0
For i = LBound(arrays) To UBound(arrays)
arr = arrays(i)
totalSize = totalSize + (UBound(arr) - LBound(arr) + 1)
Next i
' Merge arrays
ReDim result(0 To totalSize - 1)
currentIndex = 0
For i = LBound(arrays) To UBound(arrays)
arr = arrays(i)
For j = LBound(arr) To UBound(arr)
result(currentIndex) = arr(j)
currentIndex = currentIndex + 1
Next j
Next i
MergeArrays = result
End Function
Error Handling
The UBound function can raise the following errors:
- Error 9 (Subscript out of range): If the array has not been initialized (for dynamic arrays)
- Error 9 (Subscript out of range): If
dimensionis less than 1 or greater than the array's number of dimensions - Error 13 (Type mismatch): If the variable is not an array
- Error 5 (Invalid procedure call or argument): If dimension parameter is invalid
Performance Notes
- Very fast O(1) operation - directly returns array metadata
- No performance difference between dimensions
- Safe to call repeatedly in loops
- Consider caching value if used extensively in tight loops
- No memory allocation or copying involved
Best Practices
- Always use with
LBoundfor complete array bounds information - Check for initialization with On Error Resume Next for dynamic arrays
- Use in For loops instead of hardcoding array sizes
- Specify dimension explicitly for multi-dimensional arrays
- Cache in variables if used multiple times in tight loops
- Validate dimension parameter when working with multi-dimensional arrays
- Handle errors gracefully for potentially uninitialized arrays
- Use for
ParamArrayto handle variable arguments - Document array bounds in function comments
- Prefer explicit bounds in array declarations for clarity
Comparison Table
| Function | Purpose | Returns | Notes |
|---|---|---|---|
UBound |
Upper bound | Long | Largest valid index |
LBound |
Lower bound | Long | Smallest valid index |
Array |
Create array | Variant | Returns zero-based array |
ReDim |
Resize array | N/A | Statement, not function |
Platform Notes
- Available in VB6, VBA, and
VBScript - Behavior consistent across platforms
- Returns Long (32-bit signed integer)
- Maximum array size limited by available memory
- Multi-dimensional arrays limited to 60 dimensions
Limitations
- Cannot determine if array is initialized without error handling
- Does not return array capacity (allocated size vs. used size)
- No built-in way to get all dimensions at once
- Dimension parameter must be compile-time constant in some contexts
- Cannot be used on Collections or other non-array types
- Does not work with jagged arrays (arrays of arrays) directly