Article ID: 177991 - Last Review: March 29, 2007 - Revision: 3.5 XL: Limitations of Passing Arrays to Excel Using AutomationThis article was previously published under Q177991 On This PageSUMMARY This 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 a Microsoft Certified Partner or the Microsoft fee-based
consulting line at (800) 936-5200. For more information about Microsoft
Certified Partners, please visit the following Microsoft Web site: https://partner.microsoft.com/global/30000104
(https://partner.microsoft.com/global/30000104)
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
(http://support.microsoft.com/default.aspx?scid=fh;en-us;cntactms)
When 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. Version of Passing Array to Passing Array to Microsoft Excel Worksheet Range Macro (Procedure) ---------------------------------------------------------- 5.0 A B,C 7.0 (Excel 95) D E,C,H 8.0 (Excel 97) F G,H 9.0 (Excel 2000) F G,H 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: Out of Memory 1005: Unable to set the Value property of the Range class Run-time error '1004': Transpose method of Application class failed The maximum number of elements in the array is 4095. If you exceed this limit, you may receive the following error message: Run-time error '1004': Run method of Application class failed 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:
Run-time error '1004': Cannot find macro <macro name> The maximum number of elements in the array is 5461. If you exceed this limit, you receive one of the following error messages: Run-time error '1004': Transpose method of Application class failed
- or -
Run-time error '1005': Unable to set the Value property of the Range class The maximum number of elements in the array is 5461. If you exceed this limit, you receive the following error message: Run-time error '1004': Run method of Application class failed 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: Run-time error '13': Type Mismatch 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 Explicit Passing a 1-Dimensional Array to a Worksheet Range: Private xlApp As Object Private xlBook As Object Private xlSheet As Object REFERENCES For more information about arrays and Excel, please see the
following articles in the Microsoft Knowledge Base: 166342
(http://support.microsoft.com/kb/166342/EN-US/
)
XL97: Maximum Array Size in Microsoft Excel 97
For more information about getting help with Visual Basic for
Applications, please see the following article in the Microsoft Knowledge Base:
153307 (http://support.microsoft.com/kb/153307/EN-US/ ) HOWTO: Call Microsoft Excel Macros that Take Parameters 153090 (http://support.microsoft.com/kb/153090/EN-US/ ) How To Pass a Visual Basic Array to an Excel Worksheet 163435
(http://support.microsoft.com/kb/163435/EN-US/
)
VBA: Programming Resources for Visual Basic for Applications
| Article Translations
|

Back to the top
