Microsoft provides programming examples for illustration only, without warranty either expressed or implied, including, but not limited to, the implied warranties of merchantability and/or fitness for a particular purpose. This article assumes that you are familiar with the programming language being demonstrated and the tools used to create and debug procedures. Microsoft support professionals can help explain the functionality of a particular procedure, but they will not modify these examples to provide added functionality or construct procedures to meet your specific needs.
If you have limited programming experience, you may want to contact a Microsoft Certified Partner or Microsoft Advisory Services. For more information, visit these Microsoft Web sites:
Microsoft Certified Partners -
https://partner.microsoft.com/global/30000104Microsoft Advisory Services -
http://support.microsoft.com/gp/advisoryserviceFor more information about the support options that are available and about how to contact Microsoft, visit the following Microsoft Web site:
http://support.microsoft.com/default.aspx?scid=fh;EN-US;CNTACTMS
You can determine the number of dimensions in a Visual Basic for Applications array variable by requiring the routine to attempt a task with each dimension. When it attempts to do the task to a nonexistent dimension, an error will be returned. An error handler can be used to return the dimension in which the task was last successful.
One way to test a dimension is by using the LBound function. The LBound function takes two arguments, the array variable and the specific dimension, and returns the lower bounds of that dimension. If you use this function on a nonexistent dimension the function returns an error. When you use this procedure and add an error handler, you can determine the number of dimensions.
Sample Visual Basic Procedure
The following Sub procedure tests the array named Xarray by testing the lower bound of each dimension with the LBound function. Using a
For...Next loop, the Sub procedure cycles through the number
of possible array dimensions, up to 60000, until an error is generated.
An error handler takes the counter step that the loop failed on,
subtracts one (because the previous one was the last one without an error),
and displays the result in a message box.
To create and use the sample Sub procedure, use the steps in the following example:
- Start Excel.
- Press ALT+F11 to start the Visual Basic Editor.
- On the Insert menu, click Module.
- In the module sheet, type the following code:
Sub NumberOfDimensions()
'Dimensions Xarray as an array.
Dim Xarray(1 to 10, 5 to 20, 256 to 300, 8, -5 to 0)
'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(Xarray, DimNum)
Next DimNum
Exit Sub
' The error routine.
FinalDimension:
MsgBox "The array has " & DimNum - 1 & " dimensions"
End Sub
- Press ALT+F11 to return to Excel.
- On the Tools menu, click Macro, and then click Macros.
- Click NumberOfDimensions, and then click Run.
The macro returns 5, the number of dimensions in Xarray.