NativeExcel for .Net Developer's Reference

IRange.Formula Property

Gets or sets the cell's formula.

[Visual Basic]

Property Value

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.

book.Worksheets[1].Range["A1"].Value = 100;
book.Worksheets[1].Range["A2"].Formula = "=A1/2";
Val = book.Worksheets[1].Range["A2"].Value; //Val = 50
book.Worksheets(1).Range["A1").Value = 100
book.Worksheets(1).Range["A2").Formula = "=A1/2"
Val = book.Worksheets(1).Range("A2").Value 'Val = 50
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.
IWorksheet Sheet;
Sheet = book.Worksheets[1]; 
Sheet.Range["A2:F2"].Formula = Sheet.Range["A1:F1"].Formula;
Dim Sheet As IWorksheet
Sheet = book.Worksheets(1)
Sheet.Range("A2:F2").Formula = Sheet.Range("A1:F1").Formula
IWorksheet* Sheet;
Sheet = book->Worksheets->Item[1]; 
Sheet->Range->Item[S"A2:F2"]->Formula = Sheet->Range->Item[S"A1:F1"]->Formula;

See Also

IRange Interface | NativeExcel Namespace | HasFormula Property | Value Property | DisplayValue Property | ClearContents Method