Find if array is already defined


Posted by Michal Kaut on June 26, 2001 2:17 AM

This is a VBA question:
I have: Public MyArray()
At some point I initialise it: Redim MyArray(MySize)

Is there any way to test whether the array is already initialised? (I tried testing bounds, but that gives an error on a non-initialised array...)

Thanks a lot!

Posted by Dax on June 26, 2001 3:16 AM

Hi,

You can use the error to your advantage by checking the condition of the error object immediately after attempting to find Ubound(MyArray) e.g.

Public MyArray()

Sub TestArray()
On Error Resume Next
If Err.Number = 9 Then 'Subscript out of range
ReDim MyArray(10)
End If
On Error GoTo 0 'Reset error handling
End Sub

HTH,
Dax.



Posted by Dax on June 26, 2001 3:18 AM

Re: Mispost

Ooops, I neglected to include a line which actually checked the array...


Public MyArray()

Sub TestArray()
Dim ArrayCheck As Long
On Error Resume Next
ArrayCheck = UBound(MyArray)
If Err.Number = 9 Then 'Subscript out of range
ReDim MyArray(10)
End If
On Error GoTo 0 'Reset error handling
End Sub