XL: Non-Loop Array Transfer to a WorksheetLast reviewed: September 2, 1997Article ID: Q115776 |
The information in this article applies to:
SUMMARYThe most common way to transfer the contents of a named array to a worksheet is through a loop such as a For Next loop. A For Next loop indexes the array and inserts one element of the array to the destination address at a time. You can accomplish this same result without a loop using the Visual Basic, Applications Edition, FormulaArray property of the Range object.
MORE INFORMATIONMicrosoft provides examples of Visual Basic procedures 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 Visual Basic procedure is provided 'as is' and Microsoft does not guarantee that it can be used in all situations. Microsoft does not support modifications of this procedure to suit customer requirements for a particular purpose. Note that a line that is preceded by an apostrophe introduces a comment in the code--comments are provided to explain what the code is doing at a particular point in the procedure. Note also that an underscore character (_) indicates that code continues from one line to the next. You can type lines that contain this character as one logical line or you can divide the lines of code and include the line continuation character. For more information about Visual Basic for Applications programming style, see the "Programming Style in This Manual" section in the "Document Conventions" section of the "Visual Basic User's Guide." In Visual Basic, Applications Edition, the orientation of a one-dimensional array is horizontal. Therefore, if the range of cells on the worksheet that are to receive the contents of the array are also in the same orientation (that is, one row by several columns), the contents can be transferred with a single FormulaArray statement. For example:
'For a One dimensional horizontal array Sub arraydump1() 'Declares an array of size 10 Dim x(1 To 10) As Double 'Calculates random values For j = 1 To 10 x(j) = j * j Next j 'Transfers array contents to a horizontal area Range(Cells(2, 1), Cells(2, 10)).FormulaArray = x End SubThe above example works fine when the destination worksheet cells involve one row. However, when the contents of an array need to be transferred to a range of cells with a vertical, as opposed to a horizontal orientation (that is, one column by several rows), the above example is no longer valid. In the event that multiple rows of data are within the two-dimensional array, you must change the orientation of the array. To do this, you can declare the array a two-dimensional array with dimensions of several rows by one column. For example:
'For a two dimensional vertical array Sub arraydump2() 'Declares an array ten rows by one column Dim x(1 To 10, 1 To 1) As Double 'Calculates random values For j = 1 To 10 x(j, 1) = j * j Next j 'Transfers array contents to a vertical area Range(Cells(1, 2), Cells(10, 2)).FormulaArray = x End SubThe two-dimensional array shown in this example allows Visual Basic to set the orientation of the array as vertical; therefore, the array can be transferred to a worksheet without a loop.
REFERENCESFor more information about FormulaArray choose the Search button in Visual Basic Help and type:
FormulaArray |
Additional query words: 7.00 5.00
© 1998 Microsoft Corporation. All rights reserved. Terms of Use. |