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
- 12910 reads
Recent comments
5 years 36 weeks ago
6 years 22 weeks ago
6 years 34 weeks ago
6 years 37 weeks ago
6 years 38 weeks ago
6 years 43 weeks ago
6 years 52 weeks ago
7 years 2 days ago
7 years 3 days ago
7 years 3 days ago