1

I need to check if an array is empty or not, and for some reason, I'm getting an odd return value of -400877505. This was working fine with some other day just yesterday.

After declaring the variable, it's referenced in a function, but that function isn't called until later in my script.

Here's the relevant code without that function, which is IsNotInArray:

Option Explicit
Dim months_array() As Variant

MsgBox (Not months_array)

If ((Not months_array) = -1) Then
    MsgBox "init"
    ReDim Preserve months_array(1 To 2) As Variant
    months_array(1) = month_iter
    size_months_array = 2 

ElseIf IsNotInArray(month_iter, months_array) Then
    MsgBox "second" 

End If
simplycoding
  • 2,770
  • 9
  • 46
  • 91
  • https://stackoverflow.com/questions/206324/how-to-check-for-empty-array-in-vba-macro – Tim Williams Aug 20 '19 at 16:38
  • Saw that while I was writing up my script and so my code incorporates one of those answers. Still unsure what to do with this – simplycoding Aug 20 '19 at 16:43
  • Detecting if an array is empty with `Not` is a [compiler bug](https://stackoverflow.com/questions/183353/how-do-i-determine-if-an-array-is-initialized-in-vb6/54413405#comment65123_183356). It was never guaranteed to work. It might also have been affected by https://stackoverflow.com/q/57497723/11683. – GSerg Aug 20 '19 at 16:52
  • Why are you not using the IsEmpty function? – freeflow Aug 20 '19 at 17:01
  • @Freeflow Because it [does not work](https://stackoverflow.com/q/57561859/11683). – GSerg Aug 20 '19 at 17:14
  • It looks like you want to check whether your array has been allocated storage space. And, if not, then do so with `ReDim`. If this is the case, you can use the function `IsArrayAllocated`, which you can find at https://stackoverflow.com/questions/57577988/checking-for-empty-array-returns-400877505. – Domenic Aug 20 '19 at 17:27
  • @GSerg OK I see now. It does work but because the variant is an uninitialized array it is not empty. So I have to trap the error using Ubound on an uninitialized array. – freeflow Aug 20 '19 at 17:30
  • Ended up just using a Boolean variable and updating that since checking against an array is so wildly unreliable – simplycoding Aug 20 '19 at 18:01
  • Possible duplicate of [How to check for empty array in vba macro](https://stackoverflow.com/questions/206324/how-to-check-for-empty-array-in-vba-macro) – Cindy Meister Aug 21 '19 at 10:22

0 Answers0