获取数组的长度? [英] Get length of array?

查看:28
本文介绍了获取数组的长度?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试获取数组的长度,但一直收到此错误:

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

需要对象

我做错了吗?

Dim columns As Variant
columns = Array( _
"A", "ID", _
"D", "Name")
Debug.Print columns.Length  ' Error: Object required

推荐答案

数组长度:

UBound(columns)-LBound(columns)+1

UBound 本身并不是获取每个数组长度的最佳方法,因为 VBA 中的数组可以从不同的索引开始,例如 Dim arr(2 to 10)

UBound alone is not the best method for getting the length of every array as arrays in VBA can start at different indexes, e.g Dim arr(2 to 10)

UBound 仅在数组是基于 1 的情况下才会返回正确的结果(从 1 开始索引,例如 Dim arr(1 to 10).它将返回错误的结果)任何其他情况,例如 Dim arr(10)

UBound will return correct results only if the array is 1-based (starts indexing at 1 e.g. Dim arr(1 to 10). It will return wrong results in any other circumstance e.g. Dim arr(10)

有关 VBA 阵列的更多信息 在此 VBA 阵列教程中一>.

这篇关于获取数组的长度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆