下面是一段代码,我需要通过遍历传递的消息来存储有关警告消息的一些信息。传递的参数本身是一个变体,由 API 调用设置SAPListOfMessages
它返回一个数组String
。然而我注意到,每当有超过 1 个警告时,列表都是二维的,messageList(x-1)
显然会导致错误,因为它不是正确的索引。同样奇怪的是,for each
循环似乎忽略了维度,并且以某种方式只是展平数组并循环遍历它,就好像它是一维的一样。我看到的解决这个问题的唯一方法是在执行其他操作之前检查数组有多少维,因此我的问题。我无法找到任何有关获取维度数的信息 - 我只找到了有关其边界的信息。是否可以在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
是否可以在VBA中找到数组的维数?
这种方法增加了可能的维度数,60 是内置的最大值(参见注释):
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 60 ' 60 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
更多链接(谢谢@ChrisNeilson)
MS 使用阵列
大阵
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)