【问题标题】:VBA UBound for Split function vs UBound for String array用于拆分函数的 VBA UBound 与用于字符串数组的 UBound
【发布时间】:2019-12-02 12:34:07
【问题描述】:

当我在 Split 函数上使用 UBound 函数时,它返回 n-1(n 是数组的长度),但是当我在长度为 1 的数组上使用 UBound 函数时,它返回 n。

Dim arr1(1) As String, arr2 as Variant

arr1(0) = "test"
Debug.print "UBound(arr1): " & UBound(arr1)

arr2 = Split("test1/test2", "/")
Debug.print "UBound(arr2): " & UBound(arr2)

返回:

UBound(arr1): 1
UBound(arr2): 1

arr1 的长度为 1,arr2 的长度为 2,但 UBound 两者都返回 1?

【问题讨论】:

  • 不是 arr1 的长度为 2,因为数组是从零开始的吗?
  • Ubound 不是“长度”(必须),而是数组维度的最大索引。
  • @TimWilliams 我同意。我应该更好地措辞,现在删除评论。
  • ...所以 UBound 仅在下限为 1 时等于任何数组维度的长度(通常为零,除非您使用多单元格范围的 .Value 获取数组属性)

标签: vba split


【解决方案1】:

如果您不确定数组发生了什么,请使用 Watches

两个数组都有 2 个元素,位于 (0) 和 (1) 位置,这就是为什么您在两种情况下都看到答案:1。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-12-25
    • 1970-01-01
    • 2016-07-22
    • 2013-05-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多