VBA在多维数组上使用ubound

时间:2014-10-30 02:49:57

标签: arrays excel vba

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

例如

Dim arr(1 to 4, 1 to 3) As Variant

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

6 个答案:

答案 0 :(得分:49)

ubound(arr, 1) 

ubound(arr, 2) 

答案 1 :(得分:13)

您需要处理UBound的可选 Rank 参数。

Dim arr(1 To 4, 1 To 3) As Variant
Debug.Print UBound(arr, 1)  '◄ returns 4
Debug.Print UBound(arr, 2)  '◄ returns 3

更多信息:UBound Function (Visual Basic)

答案 2 :(得分: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

像这样使用:

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

输出:

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

B has 1 dimensions:
1 to 5

C has 0 dimensions:

答案 3 :(得分:0)

除了已经很好的答案之外,还要考虑这个函数来检索维度及其边界的数量,这与John's answer类似,但是看起来有点不同:

Function sizeOfArray(arr As Variant) As String
    Dim str As String
    Dim numDim As Integer

    numDim = NumberOfArrayDimensions(arr)
    str = "Array"

    For i = 1 To numDim
        str = str & "(" & LBound(arr, i) & " To " & UBound(arr, i)
        If Not i = numDim Then
            str = str & ", "
        Else
            str = str & ")"
        End If
    Next i

    sizeOfArray = str
End Function


Private Function NumberOfArrayDimensions(arr As Variant) As Integer
' By Chip Pearson
' http://www.cpearson.com/excel/vbaarrays.htm
Dim Ndx As Integer
Dim Res As Integer
On Error Resume Next
' Loop, increasing the dimension index Ndx, until an error occurs.
' An error will occur when Ndx exceeds the number of dimension
' in the array. Return Ndx - 1.
    Do
        Ndx = Ndx + 1
        Res = UBound(arr, Ndx)
    Loop Until Err.Number <> 0
NumberOfArrayDimensions = Ndx - 1
End Function

使用示例:

Sub arrSizeTester()
    Dim arr(1 To 2, 3 To 22, 2 To 9, 12 To 18) As Variant
    Debug.Print sizeOfArray(arr())
End Sub

及其输出:

Array(1 To 2, 3 To 22, 2 To 9, 12 To 18)

答案 4 :(得分:0)

循环D3方式;

Sub SearchArray()
    Dim arr(3, 2) As Variant
    arr(0, 0) = "A"
    arr(0, 1) = "1"
    arr(0, 2) = "w"

    arr(1, 0) = "B"
    arr(1, 1) = "2"
    arr(1, 2) = "x"

    arr(2, 0) = "C"
    arr(2, 1) = "3"
    arr(2, 2) = "y"

    arr(3, 0) = "D"
    arr(3, 1) = "4"
    arr(3, 2) = "z"

    Debug.Print "Loop Dimension 1"
    For i = 0 To UBound(arr, 1)
        Debug.Print "arr(" & i & ", 0) is " & arr(i, 0)
    Next i
    Debug.Print ""

    Debug.Print "Loop Dimension 2"
    For j = 0 To UBound(arr, 2)
        Debug.Print "arr(0, " & j & ") is " & arr(0, j)
    Next j
    Debug.Print ""

    Debug.Print "Loop Dimension 1 and 2"
    For i = 0 To UBound(arr, 1)
        For j = 0 To UBound(arr, 2)
            Debug.Print "arr(" & i & ", " & j & ") is " & arr(i, j)
        Next j
    Next i
    Debug.Print ""

End Sub

答案 5 :(得分:0)

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

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

enter image description here

然后运行下面的代码,会给你 2 个一维数组,即最后一列和最后一行: enter image description here

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