Is your array variant empty or empty ()?
"Empty" is an uninitialized option: IsEmpty (myVar) will return true ... And you can be fooled into thinking that you have an empty array (which is "Empty ()" and not "Empty" - try to keep after this class will be conducted a short test), because IsEmpty (myArray) also returns True.
Dim myVar as Variant 'this is currently Empty, and Ubound returns an error
Dim myArray () as variant 'this is currently Empty (), and Ubound returns an error
Redim myVar (0 to 0) ', it is no longer empty and has a valid Ubound Redim myVray (0 to 0)', it is no longer empty and has a valid Ubound
A reliable way to check myVar is TypeName (myVar) - if it is an array, the name contains brackets:
If Instr (Typename (myVar), "(")> 0 then
'we now know it is an array
If Not IsEmpty (myVar) Then
'We can now check its dimensions
If Ubound (myVar)> 0
'insert error-free code here
Endif
Endif
Endif
Full answer: "Define array variant in Excel VBA" on Excellerando page.
Nigel Heffernan Dec 02 '10 at 19:27 2010-12-02 19:27
source share