Below is a piece of code where I need to store some info about a warning message by going through messages passed. The parameter passed itself is a variant which is set by an API call to SAPListOfMessages
which returns an array of String
. What I've noticed however is that whenever there is more than 1 warning, the list is 2D and messageList(x-1)
obviously leads to an error because it's not a proper index. What's also strange is that the for each
loop seems to ignore dimensions and somehow just flatten the array and loop through it as if it were 1D. The only way around this I see is checking how many dimensions the array has before doing anything else and hence my question. I wasn't able to find any info on getting the number of dimensions - I only found info about their bounds. Is it possible to find the number of dimensions of an array in VBA? If not, how would you suggest I tackle this problem?
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
Is it possible to find the number of dimensions of an array in VBA?
This approach increments the possible dimensions count, 60 being the built in maximum (c.f. comment):
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
Further links (thx @ChrisNeilson)