Udostępnij za pośrednictwem


How to: Run Excel Calculations Programmatically

Applies to

The information in this topic applies only to the specified Visual Studio Tools for Office projects and versions of Microsoft Office.

Project type

  • Document-level projects

  • Application-level projects

Microsoft Office version

  • Excel 2003

  • Excel 2007

For more information, see Features Available by Application and Project Type.

You use a similar process to run calculations in a NamedRange control or a native Excel range object.

Running Calculations in a NamedRange Control

The following example creates a NamedRange at cell A1 and then calculates the cell. This code must be placed in a sheet class, not in the ThisWorkbook class.

To run calculations in a NamedRange control

  1. Create the named range.

    Dim NamedRange1 As Microsoft.Office.Tools.Excel.NamedRange = _
        Me.Controls.AddNamedRange(Me.Range("A1"), "NamedRange1")
    
    Microsoft.Office.Tools.Excel.NamedRange NamedRange1 =
        this.Controls.AddNamedRange(this.get_Range("A1", missing), "NamedRange1");
    
  2. Call the Calculate method of the specified range.

    NamedRange1.Calculate()
    
    NamedRange1.Calculate();
    

Running Calculations in a Native Excel Range

To run calculations in a native Excel Range

  1. Create the named range.

    Dim rng As Excel.Range = Me.Application.Range("A1")
    
    Excel.Range rng = this.Application.get_Range("A1", missing);
    
  2. Call the RangeCalculate method of the specified range.

    rng.Calculate()
    
    rng.Calculate();
    

See Also

Concepts

Working with Ranges

NamedRange Control

The Variable missing and Optional Parameters in Office Solutions