How To Pass a Visual Basic Array to an Excel WorksheetLast reviewed: July 17, 1996Article ID: Q153090 |
The information in this article applies to:
SUMMARYOne of the enhancements that Visual Basic 4.0 offers over previous versions is the ability to pass an array of values to a Microsoft Excel spreadsheet with a single command. This is done by a simple assignment of an array to a Microsoft Excel range.
MORE INFORMATION
Step-by-Step ExampleMethod 1:
The code sample above uses a hard-coded range. You can change the UBound of the Array making your code more flexible so you can specify the Starting column and Row for output on the spreadsheet. The array will then be entered irrespective of its size. Instead of simply using the range object, you incorporate the cells object to build up the relevant strings. To implement this behavior, use the following code:
Private Sub Command1_Click() Dim o As Object Dim i As Integer Dim iNumbers(1 To 10) As Integer Dim iStartRow As Integer Dim iStartCol As Integer iStartRow = 1 iStartCol = 1 For i = LBound(iNumbers) To UBound(iNumbers) iNumbers(i) = Int(Rnd * 100) + 1 Next i Set o = CreateObject("excel.application") o.Visible = True o.Workbooks.Add o.sheets("sheet1").range(o.cells(iStartRow, iStartCol).address, _ o.cells(iStartRow, ((iStartCol - 1) + _ UBound(iNumbers))).address).Value = iNumbers End Sub |
Additional reference words: 4.00 vb4win vb4all
© 1998 Microsoft Corporation. All rights reserved. Terms of Use. |