Excel中一维变体数组中的元素数 [英] Number of elements in a single dimension variant array in excel

查看:179
本文介绍了Excel中一维变体数组中的元素数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是此代码正确,用于确定Excel VBA中单个维度变量数组中的元素数量.假设我有一个名为Array1的变体数组,其中包含k个元素.

Is This code correct for determining the number of elements in a single dimension variant array in Excel VBA. Supposing I have a variant array named Array1 with k elements.

Dim n as Integer
n = UBound(Array1)

推荐答案

要获得准确的计数,您需要执行UBound - LBound + 1.这是因为数组不必从索引1到n,它们基本上可以从所需的任何索引开始.这是一个示例,它从3变为7,总共5个元素(3、4、5、6和7):

To get an accurate count, you need to do UBound - LBound + 1. This is because arrays don't have to go from index 1 to n, they can start at basically any index you want. Here's an example where it goes from 3 to 7, which is a total of 5 elements (3, 4, 5, 6, and 7):

Sub tgr()

    Dim Array1(3 To 7) As Variant
    Dim lNumElements As Long

    lNumElements = UBound(Array1) - LBound(Array1) + 1
    MsgBox lNumElements

End Sub

这篇关于Excel中一维变体数组中的元素数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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