VBA:如何查找数组的维数?

rib*_*o94 9 excel vba excel-vba

下面是一段代码,我需要通过传递消息来存储有关警告消息的一些信息.传递给它的参数是一个变量,它由API调用设置SAPListOfMessages,返回一个数组String.然而,我注意到,只要有超过1个警告,列表就是2D,messageList(x-1)显然会导致错误,因为它不是正确的索引.同样奇怪的是,for each循环似乎忽略了维度,并且不知何故只是将数组展平并循环遍历它,就好像它是1D一样.我看到的唯一方法是在做其他任何事情之前检查数组有多少维度,因此我的问题.我无法找到任何有关维度数量的信息 - 我只找到了有关其边界的信息.是否可以在VBA中找到数组的维数?如果没有,你会怎么建议我解决这个问题?

Sub getOverlapWarnings(ByRef messageList As Variant, ByRef warnings As Dictionary)

  Dim msg As Variant
  Dim x As Integer
  x = 1
 'look for an overlap warning message in the list of messages
  For Each msg In messageList
    'look for the keyword 'overlap' in the list of messages

    If InStr(1, msg, "overlap") <> 0 Then
       warnings.Add messageList(x - 1), msg
    End If
   x = x + 1
  Next msg
End Sub
Run Code Online (Sandbox Code Playgroud)

提前致谢!

T.M*_*.M. 7

是否可以在VBA中找到数组的维数?

这种方法倒计时尺寸,60000是内置的最大值:

Private Function nDim(ByVal vArray As Variant) As Long
' Purpose: get array dimension (MS)
Dim dimnum     As Long
Dim ErrorCheck As Long    ' OP: As Variant
On Error GoTo FinalDimension

For dimnum = 1 To 60000    ' 60000 being the absolute dimensions limitation 
    ErrorCheck = LBound(vArray, dimnum)
Next
' It's good use to formally exit a procedure before error handling
' (though theoretically this wouldn't needed in this special case - see comment) 
Exit Function

FinalDimension:
nDim = dimnum - 1

End Function
Run Code Online (Sandbox Code Playgroud)