- Start a new Project in Visual Basic. Form1 is created by default.
- Place a CommandButton on Form1.
- In the General Declarations section of Form1, enter this code:
Private Sub Command1_Click()
Dim oExcelApp As Object
' Create a reference to the currently running excel application
Set oExcelApp = GetObject(, "Excel.application")
' Make the Excel Application Visible.
oExcelApp.Visible = True
' Run the excel procedure
oExcelApp.run "proc", "David", 30
- Start Microsoft Excel. Book1 is created by default.
- Add a new module to the workbook.
For Excel 5.0 and 7.0: From the Insert menu, choose Macro, and select the Module Option. This will give you a new module sheet, Module1.
For Excel 97 and later: Press ALT+F11 to start the Visual Basic Editor. Click Module on the Insert menu.
- In Module1, type the following code:
Sub Proc(sParam1 As String, iParam2 As Integer)
MsgBox sParam1 & " is " & iParam2 & " Years Old"
- Leave the workbook open in Microsoft Excel and switch to your project in Visual Basic.
- From Visual Basic, press F5 to run the project. Click the command button, and you should see a dialog box appear with the text "David is 30 years old" in it.
Article ID: 153307 - Last Review: Mar 23, 2009 - Revision: 1