How do I find the number of dimensions of an array in VBA?

JoeBobJenkins

New Member
Joined
Feb 15, 2019
Messages
4
So if I have

dim varArray1() as Variant
dim varArray2() as Variant

redim varArray1(1,1,1,1)
redim varArray2(10,10,10)

How do I get something like

GetDimensions(varArray1)=4
GetDimensions(varArray2)=3
 

Some videos you may like

Excel Facts

Excel Joke
Why can't spreadsheets drive cars? They crash too often!

John_w

MrExcel MVP
Joined
Oct 15, 2007
Messages
6,431
Based on an old MS page which no longer exists.

Code:
Sub Array_Dimensions()

    Dim array1(10) As String
    Dim array2(10, 10) As String
    
    MsgBox NumberOfDimensions(array1)
    MsgBox NumberOfDimensions(array2)
    
End Sub


'Code below based on http://support.microsoft.com/kb/152288
'There is no built-in function to return the number of dimensions in an array variable
'in Microsoft Visual Basic for Applications. The easiest way to do this is by looping through
'the dimensions and using an error handler to return the number of dimensions.

Private Function NumberOfDimensions(theArray As Variant) As Long

    Dim DimNum As Long, ErrorCheck As Integer
    
    'Sets up the error handler
    On Error GoTo FinalDimension
    
    'Visual Basic for Applications arrays can have up to 60000 dimensions; this allows for that
    For DimNum = 1 To 60000
    
        'It is necessary to do something with the LBound to force it to generate an error
        ErrorCheck = LBound(theArray, DimNum)
        
    Next
    
    'The error routine
FinalDimension:
    
    NumberOfDimensions = DimNum - 1
    
End Function
 

steve the fish

Well-known Member
Joined
Oct 20, 2009
Messages
8,111
Office Version
  1. 365
Platform
  1. Windows
Heres a UDF:

Code:
Function arrDim(arr As Variant) As Long

Dim a As Long, val As Long
    
On Error GoTo Err
Do
    a = a + 1
    val = UBound(arr, a)
Loop

Err:
arrDim = a - 1

End Function
 

Watch MrExcel Video

Forum statistics

Threads
1,109,431
Messages
5,528,735
Members
409,831
Latest member
KT50

This Week's Hot Topics

  • Change military grades into rank
    Afternoon all Need help with formula that will change military rank (i.e. 1, 2, 3 into Amn, A1C, SrA). Running IF formula that does not work...
  • VBA COUNTIF SOLUTION
    Hi The following are the errors spread across the several columns from E to Q ie. 13 columns across several sheets with more than 500 rows per...
  • INSERT ROW WITH SPECIFIS TEXT IN A COLUMN
    Hi All! How can identify that that the row to be inserted has to be inserted before 1st row with specific text in column F. If I record the...
  • Auto-Create a monthly Sign in sheet for preschool students
    The image below is what each page looks like. Above is space for the "Child Name" "Month" "Class" School days are obviously Monday-Friday but...
  • VBA vlookup multiple results
    Hi folks, Hopefully someone out there can help. I have a list to vlookup which works (ish). the lookup only picks up the first instance of the...
  • Extract values for earliest/latest times
    I am trying to put together a formula to get the earliest start time, the latest end time from column A for each person in Column B-F without the...
Top