The information in this article applies to:
SUMMARYThis article discusses limitations in passing arrays to Microsoft Excel worksheets and macros using Automation. If these limitations are exceeded, you may receive run-time errors in your Automation code. MORE INFORMATIONMicrosoft 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 the Microsoft fee-based consulting line at (800) 936-5200. For more information about the support options available from Microsoft, please see the following page on the World Wide Web: http://www.microsoft.com/support/supportnet/overview/overview.aspWhen passing arrays to Microsoft Excel using Automation, the following limitations apply, depending on the version of Excel and whether you are passing the array to a worksheet range or as an argument to a macro. See the appropriate footnotes following the table for details.
FootnotesA:The maximum number of elements in the array is approximately 6550. If you exceed this limit, you receive one or both of the following error messages: The maximum number of elements in the array that you can pass to Excel using the Excel Transpose function is 4095. If you exceed this limit, you receive the following error message: B: The maximum number of elements in the array is 4095. If you exceed this limit, you may receive the following error message: NOTE: When passing a multidimensional array, you may not receive an error message, but the Excel macro will not run when the total number of elements exceeds 4095. C: When defining the array parameter in the Excel macro, it must be defined as a Variant variable or you will receive the following error message when you attempt to run the macro using Automation: For example, suppose the name of your macro in Excel is AcceptArray. Here is an example of how the array must be defined in the Excel macro:
D:
The maximum number of elements in the array is 5461. If you exceed this limit, you receive one of the following error messages:
- or - E: The maximum number of elements in the array is 5461. If you exceed this limit, you receive the following error message: F: The maximum number of elements in the array is limited by available memory or the Excel worksheet maximum size (65536 rows X 256 columns). However, the maximum number of elements in the array that you can pass to Excel using the Excel Transpose function is 5461. If you exceed this limit, you receive the following error message: G: The maximum number of elements in the array is limited only by available memory. Also, you do not have to define the parameter as a Variant variable in the Excel macro. However, if you want to pass the array ByVal, you must define the parameter as a Variant variable as in the example in footnote C earlier in this article. You will get a run-time error 13, "Type Mismatch" error, if you do not define the variable as a Variant. H: When passing an argument ByRef to an out-of-process Automation server, such as Excel, marshalling of the data is done between the Automation controller (or client) and server since they run in separate processes. This means that when an array is passed to Excel using ByRef, a copy of the array is sent to the address space of Excel. After the Excel procedure runs, a copy of the array is passed back to the client. Although this does allow for passing arguments using ByRef to an out-of-process server, it is not very efficient. On the other hand, when using an in-process automation server (a dynamic-link library (DLL)) and you pass an argument ByRef, this is very efficient since no marshalling is done. The server is using the same array in memory as the client. This is possible since the server runs in the same address space as the client. Because of marshalling with an out-of-process server such as Excel, it is more efficient to pass the array ByVal instead of ByRef. This way, only one copy is passed to Excel and Excel does not have to pass the copy back to the client. To pass an array ByVal to an Excel macro, you must define the parameter in the Excel macro as a Variant variable. See footnote C earlier in this article for an example. Sample Visual Basic ProceduresThe following sample Microsoft Visual Basic for Applications Sub procedures show how to pass arrays to Excel. The first two procedures show how to populate a range of cells on a worksheet by passing an array to a worksheet range. The third procedure shows how to pass an array to an Excel macro.The following declarations are used with each example: Option ExplicitPassing a 1-Dimensional Array to a Worksheet Range:
Passing a 2-Dimensional Array to a Worksheet Range:
Passing an Array as an Argument to an Excel Macro:
The AcceptArray procedure within a module inside C:\MyBook.xls resembles
the following:
REFERENCESFor more information about arrays and Excel, please see the following articles in the Microsoft Knowledge Base: Q75376 XL: Maximum Array Size in Microsoft ExcelFor more information about getting help with Visual Basic for Applications, please see the following article in the Microsoft Knowledge Base: Q163435 VBA: Programming Resources for Visual Basic for Applications Additional query words: 8.0 ole
Keywords : kbAutomation kbExcel KbVBA kbVBp kbOffice2000 kbExcel97 kbexcel2000 |
Last Reviewed: November 13, 1999 © 2000 Microsoft Corporation. All rights reserved. Terms of Use. |