Non-Loop Array Transfer to a Worksheet (115776)



The information in this article applies to:

  • Microsoft Excel for Windows 95
  • Microsoft Excel for Windows 5.0c

This article was previously published under Q115776

SUMMARY

The 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 INFORMATION

Microsoft 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 Sub
				
The 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 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 Sub
				
The 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.

REFERENCES

For more information about FormulaArray choose the Search button in Visual Basic Help and type:

FormulaArray


Modification Type:MinorLast Reviewed:10/11/2006
Keywords:kbProgramming KB115776