Check if iterable vba value

In many languages, you can check if an object is iterable, how to do it for VBA?

I can try:

Function isIterable(obj As Variant) As Boolean
    On Error Resume Next
    Dim iterator As Variant
    For Each iterator In obj
        Exit For
    Next
    isIterable = Err.Number = 0
End Function

But I wonder if there is a built-in or better approach?

+6
source share
2 answers

Is there a built-in function: None.

Is there a better approach ?:

I would do it like this:

Function isIterable(obj As Variant) As Boolean

    On Error GoTo isIterable_Error

    Dim iterator As Variant

    For Each iterator In obj
        isIterable = True
        Exit Function
    Next

isIterable_Error:

End Function

Because adding twice =to one line is too much.

+4
source

I don't think this is better than the Vityata function, but as an alternative:

Function isIterable(obj As Object) As Boolean

    On Error Resume Next

    isIterable = TypeName(CallByName(obj, "_NewEnum", VbGet)) = "Unknown"
    If Not isIterable Then isIterable = TypeName(CallByName(obj, "_NewEnum", VbMethod)) = "Unknown"

End Function
+2
source

All Articles