HOWTO: Transfer an Array from VB 3.0 to a Microsoft Excel Sheet |
Q114258
This article discusses how you can use Microsoft Excel version 5.0, Visual Basic version 3.0, and OLE automation to transfer the contents of an array from a Visual Basic application to a Microsoft Excel worksheet.
OLE Automation allows you use a Visual Basic application to control another application, such as Microsoft Excel. In order to manipulate the object of another application, you need to identify it with a programmatic identifier. Programmatic identifiers are registered in your system when you install an application. The programmatic identifiers for Microsoft Excel 5.0 are listed in the following table:
Represents this
This identifier Microsoft Excel 5.0 object
-----------------------------------------------------
Excel.Application.5 Application
Excel.Sheet.5 Sheet
Excel.Chart.5 Chart
The following example demonstrates how to create a Visual Basic version 3.0
executable program that can be run from a Microsoft Excel Visual Basic
macro to transfer the contents of an array to the active worksheet in
Microsoft Excel.
Sub Form_Click ()
Dim XLApp As object
Dim WBook As object
Dim WSheet As object
Dim TestArray() As Integer
Dim i As Integer
'Assuming Excel 5.0 is currently running, set XLApp to the
'Excel 5.0 Application
Set XLApp = GetObject(, "Excel.application.5")
'Set WBook to the Activeworkbok in Excel
Set WBook = XLApp.Activeworkbook
'Set WSheet to the Activesheet in Excel
Set WSheet = XLApp.Activesheet
'Create a two dimensional array containing random values
ReDim TestArray(1 To 10, 1 To 2) As Integer
For i = 1 To 10
TestArray(i, 1) = Rnd * 10
TestArray(i, 2) = Rnd * 1000
Next i
'Transfer the values in TestArray to the range A1:B10 on the
' ActiveSheet in Excel
For i = 1 To UBound(TestArray)
WSheet.Range("A" & i).Value = TestArray(i, 1)
WSheet.Range("B" & i).Value = TestArray(i, 2)
Next i
'Display the message that the transfer was complete and to
close
' the form
MsgBox "TestArray has been transferred to [" & WBook.Name & "]"
& _
WSheet.Name "."
Unload Form1
End Sub
Sub TransferArrayFromVBApp()
'Start the VB application Test.Exe in a normal window with focus
Shell "C:\vb\test.exe", 1
End Sub "Visual Basic User's Guide," version 5.0, Chapter 10 "Microsoft Visual Basic Programmer's Guide," version 3.0, Chapter 23
Additional query words: xl5 Excel W_Excel
Keywords : kbinterop kbprg kbExcel kbVBp300 kbWord kbDSupport kbhowto
Issue type : kbhowto
Technology :
|
Last Reviewed: December 17, 1999 © 2001 Microsoft Corporation. All rights reserved. Terms of Use. |