Home
Search
 
What's New
Index
Books
Links
Q & A
Newsletter
Banners
 
Feedback
Tip Jar
 
C# Helper...
 
XML RSS Feed
Follow VBHelper on Twitter
 
 
MSDN Visual Basic Community
 
 
 
 
 
TitleEvaluate mathematical expressions using Excel
KeywordsOffice, Eexcel, expression, evaluate
CategoriesOffice
 
Open an Excel server. Insert the equation into a cell and read the result.
 
Private Sub cmdEvaluate_Click()
Dim excel_app As Object
Dim excel_sheet As Object

    Set excel_app = CreateObject("Excel.Application")

    ' Uncomment this line to make Excel visible.
'    excel_app.Visible = True

    ' Check for later versions.
    excel_app.Workbooks.Add
    If Val(excel_app.Application.Version) >= 8 Then
        Set excel_sheet = excel_app.ActiveSheet
    Else
        Set excel_sheet = excel_app
    End If

    ' Insert the equation.
    excel_sheet.Cells(1, 1) = _
        "=" & txtExpression.Text

    ' Get the result.
    lblResult.Caption = excel_sheet.Cells(1, 1)

    ' Comment the rest of the lines to keep
    ' Excel running so you can see it.

    ' Close the workbook without saving.
    excel_app.ActiveWorkbook.Close False

    ' Close Excel.
    excel_app.Quit
    Set excel_sheet = Nothing
    Set excel_app = Nothing
End Sub
 
 
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated