Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find the number of dimensions that an array has?

Tags:

excel

vba

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
like image 625
ribarcheto94 Avatar asked Jun 09 '18 17:06

ribarcheto94


People also ask

What are the dimensions of an array?

The dimensionality of an array is how many axes it has. You index into it with one subscript, e.g. array[n] . You index into it with two subscripts, e.g. array[x,y] .

How many dimensions are there in array in C?

In C programming, an array can have two, three, or even ten or more dimensions. The maximum dimensions a C program can have depends on which compiler is being used.

How do you find the number of dimensions in a NumPy array?

Use ndim attribute available with numpy array as numpy_array_name. ndim to get the number of dimensions. Alternatively, we can use shape attribute to get the size of each dimension and then use len() function for the number of dimensions.

Which command gives the dimension of an array A?

Description. sz = size( A ) returns a row vector whose elements are the lengths of the corresponding dimensions of A . For example, if A is a 3-by-4 matrix, then size(A) returns the vector [3 4] .


1 Answers

An array has 2 bounds: Upper and Lower.

I think you're asking where the lower bound begins.

By default, the lower bound is zero. For example:

Sub test()
    Dim arr
    arr = Array("a", "b", "c")
    Debug.Print "Lower: " & LBound(arr), "Upper: " & UBound(arr)
End Sub

returns: Lower: 0 Upper: 2 because the 3 elements have indices of 0, 1, and 2.


Some functionality may begin at 1 by default but it's rare. One example is filling an array with a range:

Sub test()
    Dim arr
    arr = Range("A2:A4")
    Debug.Print "Lower: " & LBound(arr), "Upper: " & UBound(arr)
End Sub

...returns: Lower: 1 Upper: 3


If you fully declare the array, you can make the upper and lower bound whatever you want:

Sub test()
    Dim arr(99 To 101) As String
    arr(100) = "blah"
    Debug.Print "Lower: " & LBound(arr), "Upper: " & UBound(arr)
End Sub

...returns: Lower: 99 Upper: 101, but an array with declared bounds won't work with many functions (like the previous examples.


You can also set the default lower bound with an statement at the very top of each module:

Option Base 1

...but there are so many places it doens't apply it's kind of useless. (More here.)


See also:

  • MSDN : Declaring Arrays (Fixed & Dynamic)

  • MSDN : LBound Function

  • MSDN : UBound Function

like image 161
ashleedawg Avatar answered Oct 18 '22 08:10

ashleedawg