VBA在多维数组上使用ubound

bsa*_*aka 32 arrays excel vba

Ubound可以返回数组的最大索引值,但在多维数组中,我如何指定WHICH维度我想要的最大索引?

例如

Dim arr(1 to 4, 1 to 3) As Variant
Run Code Online (Sandbox Code Playgroud)

在这个4x3阵列中,我如何获得Ubound返回4,以及如何获得Ubound返回3?

小智 15

你需要处理可选的Rank参数UBound.

Dim arr(1 To 4, 1 To 3) As Variant
Debug.Print UBound(arr, 1)  '? returns 4
Debug.Print UBound(arr, 2)  '? returns 3
Run Code Online (Sandbox Code Playgroud)

更多:UBound函数(Visual Basic)


Joh*_*man 5

[这是一个迟到的答案,解决了问题的标题(因为这是人们在搜索时会遇到的),而不是OP问题的具体细节已经得到了充分的回答]

Ubound有点脆弱,因为它无法知道数组有多少维度.您可以使用错误捕获来确定数组的完整布局.以下内容返回一组数组,每个维度一个.该count属性可用于确定维数,并可根据需要提取其下限和上限:

Function Bounds(A As Variant) As Collection
    Dim C As New Collection
    Dim v As Variant, i As Long

    On Error GoTo exit_function
    i = 1
    Do While True
        v = Array(LBound(A, i), UBound(A, i))
        C.Add v
        i = i + 1
    Loop
exit_function:
    Set Bounds = C
End Function
Run Code Online (Sandbox Code Playgroud)

像这样使用:

Sub test()
    Dim i As Long
    Dim A(1 To 10, 1 To 5, 4 To 10) As Integer
    Dim B(1 To 5) As Variant
    Dim C As Variant
    Dim sizes As Collection

    Set sizes = Bounds(A)
    Debug.Print "A has " & sizes.Count & " dimensions:"
    For i = 1 To sizes.Count
        Debug.Print sizes(i)(0) & " to " & sizes(i)(1)
    Next i

    Set sizes = Bounds(B)
    Debug.Print vbCrLf & "B has " & sizes.Count & " dimensions:"
    For i = 1 To sizes.Count
        Debug.Print sizes(i)(0) & " to " & sizes(i)(1)
    Next i

    Set sizes = Bounds(C)
    Debug.Print vbCrLf & "C has " & sizes.Count & " dimensions:"
    For i = 1 To sizes.Count
        Debug.Print sizes(i)(0) & " to " & sizes(i)(1)
    Next i
End Sub
Run Code Online (Sandbox Code Playgroud)

输出:

A has 3 dimensions:
1 to 10
1 to 5
4 to 10

B has 1 dimensions:
1 to 5

C has 0 dimensions:
Run Code Online (Sandbox Code Playgroud)


Vit*_*ata 5

  • UBound(myArray, 1) 返回二维数组中的行数
  • UBound(myArray, 2) 返回二维数组中的列数

但是,让我们更进一步,假设您需要范围的最后一行和最后一列,它已被写入为二维数组。该行(或列)应转换为一维数组。例如,如果我们的二维数组如下所示:

在此输入图像描述

然后运行下面的代码,将为您提供 2 个一维数组,即最后一列和最后一行: 在此输入图像描述

Sub PrintMultidimensionalArrayExample()    
    Dim myRange As Range: Set myRange = Range("a1").CurrentRegion        
    Dim myArray As Variant: myArray = myRange        
    Dim lastRowArray As Variant: lastRowArray = GetRowFromMdArray(myArray, UBound(myArray, 1))
    Dim lastColumnArray As Variant
    lastColumnArray = GetColumnFromMdArray(myArray, UBound(myArray, 2))        
End Sub

Function GetColumnFromMdArray(myArray As Variant, myCol As Long) As Variant        
    'returning a column from multidimensional array
    'the returned array is 0-based, but the 0th element is Empty.        
    Dim i As Long
    Dim result As Variant
    Dim size As Long: size = UBound(myArray, 1)
    ReDim result(size)        
    For i = LBound(myArray, 1) To UBound(myArray, 1)
        result(i) = myArray(i, myCol)
    Next        
    GetColumnFromMdArray = result        
End Function

Function GetRowFromMdArray(myArray As Variant, myRow As Long) As Variant        
    'returning a row from multidimensional array
    'the returned array is 0-based, but the 0th element is Empty.        
    Dim i As Long
    Dim result As Variant
    Dim size As Long: size = UBound(myArray, 2)
    ReDim result(size)        
    For i = LBound(myArray, 2) To UBound(myArray, 2)
        result(i) = myArray(myRow, i)
    Next        
    GetRowFromMdArray = result        
End Function
Run Code Online (Sandbox Code Playgroud)