How to check if an array is totally empty

Glory

Well-known Member
Joined
Mar 16, 2011
Messages
640
I've declared a public array at the module level. I need to know what statement will check the array to see whether it has any values assigned to it.

"Isempty" doesn't work. Neither does "IsNull". Any ideas?
 

Excel Facts

What is the last column in Excel?
Excel columns run from A to Z, AA to AZ, AAA to XFD. The last column is XFD.
A dynamic array or a static array?

Of what data type?
 
Upvote 0
I use

Code:
If Join(Arr) <> "" Then ... 'The array contains some values

It basically puts all of the elements together into a string so if there is at least one value then the string will not be null.
 
Upvote 0
I use

Code:
If Join(Arr) <> "" Then ... 'The array contains some values
It basically puts all of the elements together into a string so if there is at least one value then the string will not be null.
This doesn't work for me. May have to check element by element:
Code:
Option Base 1
Dim vArr(10) As Variant
Sub myMod()
For i = 1 To UBound(vArr, 1)
    If IsEmpty(vArr(i)) Then Ct = Ct + 1
Next i
If Ct = UBound(vArr, 1) Then
    MsgBox "Array is empty"
Else
    MsgBox "Array is not empty"
End If
End Sub
 
Upvote 0
Sorry, JoeMo, you are correct.

What I posted I use to keep UBound() from erroring out on an array that I have dimmed without a size. I use this if I am filling an array with an unknown number of elements. I just Dim vArr() as Variant and then ReDim vArr(i) in a loop when adding the data to the array. If I later want to loop through the array and try using ubound in my loop it causes an error if for some reason the redim part of my code wasn't executed, like if there wasn't any data that met the criteria to go into the array. I found Join() is the only way to prevent this. Use it before the loop using ubound and it will bypass the loop if there are no elements in the array.

I thought it might work with an empty array as well, but it appears that an array with dimensions isn't really empty, so Join() makes a string out of those characters.
 
Upvote 0
If you just want to know if it is empty, you could simplify the code and use something like this. You don't have to count the non empty elements.

Code:
Dim vArr(10) As Variant
Sub myMod()
For i = 1 To UBound(vArr, 1)
    If Not IsEmpty(vArr(i)) Then
        MsgBox "Array is not empty"
        Exit For
    End If
Next i
 
Upvote 0
Code:
'http://www.mrexcel.com/forum/showthread.php?t=330424
Public Function IsBounded(vArray As Variant) As Boolean

    'If the variant passed to this function is an array, the function will return True;
    'otherwise it will return False
    On Error Resume Next
    IsBounded = IsNumeric(UBound(vArray))

End Function
 
Upvote 0
Dynamic array, with um... Long data, I think. 5- to 7-digit numbers.
Ones the long type dynamic array is dimensioned it contains zero values.
Seems that you are interested in a way to check if the dynamic array has been dimensioned (bounded) or not.
It’s just for fun, but the method works with dynamic array variables:
Rich (BB code):

Dim MyArr() ' As Long

Function IsDimentioned(Arr) As Boolean
  IsDimentioned = (Not MyArr) <> -1
End Function
 
Sub Test()
  Debug.Print IsDimentioned(MyArr)
  ReDim MyArr(1 To 7)
  Debug.Print IsDimentioned(MyArr)
End Sub

If variant variable is used instead of any type of array variable then it doesn’t work - use the Kenneth’ solution for any cases.
 
Last edited:
Upvote 0

Forum statistics

Threads
1,224,599
Messages
6,179,827
Members
452,946
Latest member
JoseDavid

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top