Get length of array?

前端 未结 5 1001
北海茫月
北海茫月 2020-12-13 08:13

I\'m trying to get the length of an array, yet I keep getting this error:

Object required

Am I doing something wrong?

         


        
5条回答
  •  轮回少年
    2020-12-13 08:55

    Compilating answers here and there, here's a complete set of arr tools to get the work done:

    Function getArraySize(arr As Variant)
    ' returns array size for a n dimention array
    ' usage result(k) = size of the k-th dimension
    
    Dim ndims As Long
    Dim arrsize() As Variant
    ndims = getDimensions(arr)
    ReDim arrsize(ndims - 1)
    For i = 1 To ndims
        arrsize(i - 1) = getDimSize(arr, i)
    Next i
    getArraySize = arrsize
    End Function
    
    Function getDimSize(arr As Variant, dimension As Integer)
    ' returns size for the given dimension number
        getDimSize = UBound(arr, dimension) - LBound(arr, dimension) + 1
    End Function
    
    Function getDimensions(arr As Variant) As Long
    ' returns number of dimension in an array (ex. sheet range = 2 dimensions)
        On Error GoTo Err
        Dim i As Long
        Dim tmp As Long
        i = 0
        Do While True
            i = i + 1
            tmp = UBound(arr, i)
        Loop
    Err:
        getDimensions = i - 1
    End Function
    

提交回复
热议问题