Notice: This website is an unofficial Microsoft Knowledge Base (hereinafter KB) archive and is intended to provide a reliable access to deleted content from Microsoft KB. All KB articles are owned by Microsoft Corporation. Read full disclaimer for more details.

XL2000: Determining the Number of Dimensions in an Array Variable


View products that this article applies to.

This article was previously published under Q213273

↑ Back to the top


Summary

In Microsoft Excel, there is no built-in function to return the number of dimensions in an array variable in Microsoft Visual Basic for Applications. One way to do this is by looping through the dimensions and using an error handler to return the number of dimensions.

↑ Back to the top


More information

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/30000104

Microsoft Advisory Services - http://support.microsoft.com/gp/advisoryservice

For 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:
  1. Start Excel.
  2. Press ALT+F11 to start the Visual Basic Editor.
  3. On the Insert menu, click Module.
  4. 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
    						
  5. Press ALT+F11 to return to Excel.
  6. On the Tools menu, click Macro, and then click Macros.
  7. Click NumberOfDimensions, and then click Run. The macro returns 5, the number of dimensions in Xarray.

↑ Back to the top


References

For more information about how to use the sample code in this article, click the article number below to view the article in the Microsoft Knowledge Base:
212536� OFF2000: How to Run Sample Code from Knowledge Base Articles

↑ Back to the top


Keywords: KB213273, kbprogramming, kbhowto, kbdtacode

↑ Back to the top

Article Info
Article ID : 213273
Revision : 8
Created on : 11/23/2006
Published on : 11/23/2006
Exists online : False
Views : 288