Freigeben über


NamedRange.Formula Property

Definition

Gets or sets the NamedRange control's formula in A1-style notation.

public:
 property System::Object ^ Formula { System::Object ^ get(); void set(System::Object ^ value); };
public object Formula { get; set; }
member this.Formula : obj with get, set
Public Property Formula As Object

Property Value

The NamedRange control's formula in A1-style notation.

Examples

The following code example sets the Formula property of a NamedRange control to calculate the sum of cells A1 through A5, uses the FormulaHidden property to hide the formula, and then calls the Calculate method to calculate the sum of the cells and place the sum in cell A6.

This example is for a document-level customization.

private void CalculateRange()
{
    Microsoft.Office.Tools.Excel.NamedRange namedRange1 =
        this.Controls.AddNamedRange(this.Range["A1", "A5"],
        "namedRange1");

    Microsoft.Office.Tools.Excel.NamedRange namedRange2 =
        this.Controls.AddNamedRange(this.Range["A6"],
        "namedRange2");

    namedRange1.Value2 = 5;
    namedRange2.Formula = "=SUM(A1:A5)";
    namedRange2.FormulaHidden = true;
    namedRange2.Calculate();
}
Private Sub CalculateRange()
    Dim namedRange1 As Microsoft.Office.Tools.Excel.NamedRange _
        = Me.Controls.AddNamedRange(Me.Range("A1", "A5"), _
        "namedRange1")

    Dim namedRange2 As Microsoft.Office.Tools.Excel.NamedRange _
        = Me.Controls.AddNamedRange(Me.Range("A6"), _
        "namedRange2")

    namedRange1.Value2 = 5
    namedRange2.Formula = "=SUM(A1:A5)"
    namedRange2.FormulaHidden = True
    namedRange2.Calculate()
End Sub

Remarks

If the NamedRange control contains a constant, this property returns the constant. If the NamedRange control is empty, this property returns an empty string. If the NamedRange control contains a formula, this property returns the formula as a string in the same format that would be displayed in the formula bar (including the equal sign).

If you set the value or formula of a NamedRange control to a date, Microsoft Office Excel checks to see whether that cell is already formatted with one of the date or time number formats. If not, Excel changes the number format to the default short date number format.

If the range is a one-dimensional or two-dimensional range, you can set the formula to an array of the same dimensions. Similarly, you can put the formula into an array.

Setting the formula for a multiple-cell NamedRange control fills all cells in the control with the formula.

Applies to