Count Array Dimensions
The following piece of code gives the number of dimensions an array has. In the TestRun procedure a range is assigned to variant and that variant is passed to GetArrayDimensions function as a parameter. GetArrayDimensions function returns the number of dimensions in an array. If the range has only one cell then it returns 0 as its not an array; if it has more than one cell its an array in which case it returns 2.
Sub TestRun() Dim arr arr = Sheet1.Range("A1:A2") MsgBox "No of Dimensions in Array: " & GetArrayDimensions(arr), vbInformation, "Excel Experts" End Sub Function GetArrayDimensions(arrVariant) As Integer Dim lngDimCnt As Long Dim lngChkErr As Long On Error GoTo ExitF For lngDimCnt = 1 To 60000 'max 60000 dimensions allowed lngChkErr = LBound(arrVariant, lngDimCnt) Next lngDimCnt Exit Function ExitF: GetArrayDimensions = lngDimCnt - 1 End Function
»
- Vishesh's blog
- Login or register to post comments
- 12901 reads
Recent comments
5 years 34 weeks ago
6 years 20 weeks ago
6 years 32 weeks ago
6 years 35 weeks ago
6 years 36 weeks ago
6 years 42 weeks ago
6 years 50 weeks ago
6 years 50 weeks ago
6 years 50 weeks ago
6 years 50 weeks ago