Delen via


How to: Programmatically run Excel calculations

Applies to: yesVisual Studio noVisual Studio for Mac

Note

This article applies to Visual Studio 2017. If you're looking for the latest Visual Studio documentation, see Visual Studio documentation. We recommend upgrading to the latest version of Visual Studio. Download it here

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

Applies to: The information in this topic applies to document-level projects and VSTO Add-in projects for Excel. For more information, see Features available by Office application and project type.

Run 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.

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

    NamedRange1.Calculate();
    
    NamedRange1.Calculate()
    

Run calculations in a native Excel range

To run calculations in a native Excel range

  1. Create the named range.

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

    rng.Calculate();
    
    rng.Calculate()
    

See also