ID: Q114258
The information in this article applies to:
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.
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."
1. Start Visual Basic version 3.0.
2. Enter the following code for the Click event for Form1:
'***** VISUAL BASIC 3.0 EXAMPLE PROCEDURE ********
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
3. From the File menu, choose Make EXE File to compile the code.
4. Quit Visual Basic.
5. Start Microsoft Excel 5.0.
6. From the File menu, choose New.
7. From the Insert menu, choose Macro, and then choose Module.
8. Enter the following code on the Visual Basic module (Note this example
assumes the .EXE file created in step 3 above is located on
C:\VB\TEST.EXE):
'*****EXCEL 5.0 VISUAL BASIC MACRO EXAMPLE******
Sub TransferArrayFromVBApp()
'Start the VB application Test.Exe in a normal window with focus
Shell "C:\vb\test.exe", 1
End Sub
9. Select "Sheet1" in the active workbook.
10. From the Tools menu, choose Macro.
11. Select the TransferArrayFromVBApp macro, and then choose the Run
button.
12. Form1 will appear. Click the form to start the transfer of the array
to the active worksheet ("Sheet1"). When the transfer is complete, a
message box stating that the transfer is complete will appear. Choose
OK.
There will be a random sequence of numbers in the range A1:B10 on Sheet1;
these numbers were transferred by the Visual Basic program.
"Visual Basic User's Guide," version 5.0, Chapter 10 "Microsoft Visual Basic Programmer's Guide," version 3.0, Chapter 23
Additional query words: 5.00 xl5 Excel W_Excel 3.00 3.0
Keywords : kbinterop kbprg kbhowto
Version : 5.00
Platform : WINDOWS
Last Reviewed: September 3, 1997