Validating Dynamic Arrays

Validating Dynamic Arrays

When you have a dynamic array that has no dimensions defined yet in VB, trying to get its lower or upper bound raises an error (#9 – Subscript out of range). There are a number of ways to check whether a variable contains a valid array or not, but the function below should fit the bill:

Public Function IsValidArray(ByVal Variable As Variant) As BooleanDim Result As BooleanDim Bol()  As BooleanDim Itg()  As IntegerDim Lng()  As LongDim Cur()  As CurrencyDim Sng()  As SingleDim Dbl()  As DoubleDim Dat()  As DateDim Stn()  As String    If Not IsArray(Variable) Then Exit Function        Select Case VarType(Variable) Xor vbArray        Case vbBoolean            Bol() = Variable            Result = Not Not Bol()                    Case vbByte            Result = LenB(CStr(Variable))  0                Case vbInteger            Itg() = Variable            Result = Not Not Itg()        Case vbLong            Lng() = Variable            Result = Not Not Lng()                    Case vbCurrency            Cur() = Variable            Result = Not Not Cur()                    Case vbSingle            Sng() = Variable            Result = Not Not Sng()                    Case vbDouble            Dbl() = Variable            Result = Not Not Dbl()                    Case vbDate            Dat() = Variable            Result = Not Not Dat()                    Case vbString            Stn() = Variable            Result = Not Not Stn()                    Case vbError, vbDecimal, vbVariant, vbObject, vbUserDefinedType            Result = LBound(Variable) 


Share the Post: