Gets or sets the cell's formula.
[Visual Basic] Property Formula As Object [C#] object Formula { get; set; } [C++] __property Object* get_Formula(); __property void set_Formula(Object* ); [JScript] function get Formula() : Object function set Formula(Object);
If the cell contains a constant, this property returns the constant. If the cell contains a formula, the Formula property returns the formula as a string (including the equal sign). If the range is a one- or two-dimensional range, you can set the formula to an object array of the same dimensions. Similarly, the Formula property returns the object array. Setting the formula for a multiple-cell range fills all cells in the range with the formula.
This example sets the formula of cell A2 and obtains the result of formula into Val variable.
[C#]
book.Worksheets[1].Range["A1"].Value = 100;
book.Worksheets[1].Range["A2"].Formula = "=A1/2";
Val = book.Worksheets[1].Range["A2"].Value; //Val = 50
[Visual Basic]
book.Worksheets(1).Range["A1").Value = 100
book.Worksheets(1).Range["A2").Formula = "=A1/2"
Val = book.Worksheets(1).Range("A2").Value 'Val = 50
[C++]
book->Worksheets->Item[1]->Range->Item[S"A1"]->Value = 100;
book->Worksheets->Item[1]->Range->Item[S"A2"]->Formula = S"=A1/100";
Val = book->Worksheets->Item[1]->Range->Item[S"A2"]->Value; //Val = 50
This example copies the formulas from A1:F1 range to A2:F2 range. [C#]
IWorksheet Sheet;
Sheet = book.Worksheets[1];
Sheet.Range["A2:F2"].Formula = Sheet.Range["A1:F1"].Formula;
[Visual Basic]
Dim Sheet As IWorksheet
Sheet = book.Worksheets(1)
Sheet.Range("A2:F2").Formula = Sheet.Range("A1:F1").Formula
[C++]
IWorksheet* Sheet;
Sheet = book->Worksheets->Item[1];
Sheet->Range->Item[S"A2:F2"]->Formula = Sheet->Range->Item[S"A1:F1"]->Formula;
IRange Interface | NativeExcel Namespace | HasFormula Property | Value Property | DisplayValue Property | ClearContents Method