Excel Application 2

Enter your search terms Submit search form




 In this example, I continue to  show you how to use the formulae in Microsoft Excel to perform arithmetic calculation. In order to integrate Microsoft Excel into Visual Basic, you need to have Microsoft Excel installed in your computer. The next step is to click on the Reference from the project menu and then check on the  Microsoft Excel 11.0 Object Library (If you are using other versions of MS Office, you may have something like  Microsoft Excel 9.0 Object Library, but it should be OK).

In the program, first of all we need to create the Excel application by using the Excel.Application declaration. Then we can enter the values into the cells using the text boxes. Finally we can use the Excel formula to calculate the average of the two numbers.

 .Public xlsAppl As Excel.Application

Private Sub Command1_Click()
Set xlsAppl = Excel.Application
xlsAppl.Cells(1, 1).Value = Val(Text1)
xlsAppl.Cells(2, 1).Value = Val(Text2)
xlsAppl.Cells(3, 1).Formula = "=AVERAGE(A1:A2)"
Label1.Caption = xlsAppl.Cells(3, 1).Value

End Sub






 [Back to VBToday]