VBA 数组长度(不是 ubound 或 onerror!)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16438902/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 15:35:15  来源:igfitidea点击:

VBA array length (not ubound or onerror!)

vbams-access

提问by user2361340

Sorry to ask such a basic question but this is driving me mad...

很抱歉问这样一个基本问题,但这让我发疯......

What function in VBA returns the number of elements in an array... i.e when the array is empty it will return 0?

VBA 中的什么函数返回数组中元素的数量...即,当数组为空时,它将返回 0?

I cant do this with UBound because it throws an error when called on an empty array and i cant believe the way to do this by using OnError to first determine if its empty or not... as is suggested on forums! array.Length complains about a bad qualifier or something.

我无法使用 UBound 执行此操作,因为它在调用空数组时会引发错误,而且我无法相信通过使用 OnError 首先确定其是否为空来执行此操作的方法......正如论坛上所建议的那样!array.Length 抱怨一个错误的限定符或其他东西。

I really need to do this:

我真的需要这样做:

dim termAry() as String
populate termAry
...

private sub populate(terms() as String)
   redim preserve terms(terms.Length) ' Redim takes ubound for array size
   terms(ubound(terms)) = "something really annoying"
end sub

P.S any good links to a concise set of VBA language and function reference would be most useful... MSDN seems really obscure!!!

PS 任何指向一套简洁的 VBA 语言和函数参考的好的链接都将是最有用的... MSDN 似乎真的很晦涩!!!

回答by Joe

I believe the only way to do this is to use On Errorand handle the Subscript Out of Rangeerror that will be raised if the array (or the dimension of the array you're interested in) isn't initialized.

我相信这样做的唯一方法是使用On Error和处理Subscript Out of Range如果数组(或您感兴趣的数组的维度)未初始化将引发的错误。

E.g.

例如

Public Function IsInitialized(arr() As String) As Boolean
    On Error GoTo ErrHandler
    Dim nUbound As Long
    nUbound = UBound(arr)
    IsInitialized = True
    Exit Function
ErrHandler:
    Exit Function
End Function

Dim a() As String
Dim b(0 To 10) As String

IsInitialized(a) ' returns False
IsInitialized(b) ' returns True

You can generalized this to test how many dimensions there are in an array, e.g.

您可以将其概括为测试数组中有多少维,例如

Public Function HasAtLeastNDimensions(arr() As String, NoDimensions As Long) As Boolean
    On Error GoTo ErrHandler
    Dim nUbound As Long
    nUbound = UBound(arr, NoDimensions)
    HasAtLeastNDimensions = True
    Exit Function
ErrHandler:
    Exit Function
End Function

Dim a() As String
Dim b(0 To 10) As String
Dim c(0 To 10, 0 To 5) As String

HasAtLeastNDimensions(a, 1) ' False: a is not initialized
HasAtLeastNDimensions(b, 1) ' True: b has 1 dimension
HasAtLeastNDimensions(b, 2) ' False: b has only 1 dimension
HasAtLeastNDimensions(c, 2) ' True: c has 2 dimensions

UPDATE

更新

In response to comment:

回应评论:

am i right in thinking that the function cannot be easily generalised to operate on any array type

我认为该函数不能很容易地推广到对任何数组类型进行操作是否正确

It can be easily generalized by making the parameter a Variant, and checking it is an array in the body of the function using the IsArrayfunction:

通过将参数设为 Variant,并使用IsArray函数检查它是函数体中的数组,可以轻松地将其泛化:

Public Function HasAtLeastNDimensions(arr As Variant, NoDimensions As Long) As Boolean
    On Error GoTo ErrHandler
    Dim nUbound As Long
    If Not IsArray(arr) Then Exit Function
    nUbound = UBound(arr, NoDimensions)
    HasAtLeastNDimensions = True
    Exit Function
ErrHandler:
    Exit Function
End Function