VB and VBA Users Source Code: Safely checking the bounds of an array
[
Home
|
Contents
|
Search
|
Reply
| Previous | Next ]
VB/VBA Source Code
Safely checking the bounds of an array
By:
Andrew Baker
Email (spam proof):
Email the originator of this post
Date:
Sunday, September 07, 2003
Hits:
1687
Category:
Visual Basic General
Article:
Using the Ubound and Lbound functions on an uninitialised typed array causes a "Subscript out of range" error. Use the following simple functions to safely check the bounds of any variant datatype. 'Purpose : Replacement function to UBound. Returns Upper bound of an array. 'Inputs : avValues The array to determine the upper bound of ' lDimension The dimension of the array ' [lDefault] The value to return if an error occurs 'Outputs : The upper bound specific dimension of avValues 'Author : Andrew Baker 'Date : 23/07/2000 19:07 'Notes : This function uses UBound with an error handler and allows you to specify a default return value 'Revisions : Function UUBound(avValues As Variant, Optional lDimension As Long = 1, Optional lDefault As Long = -1) As Long On Error GoTo ErrFailed UUBound = UBound(avValues, lDimension) Exit Function ErrFailed: 'Error Occurred, Return the Default and clear error UUBound = lDefault On Error GoTo 0 End Function 'Purpose : Replacement function for LBound (has extra functionality and error handling) 'Inputs : avValues The array to find the LBound. ' lDimension Which dimension's lower bound to return ' [lDefault] The value to return if an error occurs 'Outputs : The lower bound of the array, or lDefault if an error occurs 'Author : Andrew Baker 'Date : 25/03/2000 'Notes : LBound raises and error if you try and check the the lower bound ' of a dimension that doesn't exist. Function LLBound(avValues, Optional lDimension As Long = 1, Optional lDefault As Long = -1) As Long On Error GoTo ErrFailed LLBound = LBound(avValues, lDimension) Exit Function ErrFailed: LLBound = lDefault On Error GoTo 0 End Function
Terms and Conditions
Support this site
Download a trial version of the Excel Workbook Rebuilder