Skip to main content
.NET Framework 4.5.2+

How to: Add Formulas to Cells

To add a formula to a cell, use the CellRange.Formula property of the Cell object. Start your formula with the “=” sign.

// Use constants and calculation operators in a formula.
workbook.Worksheets[0].Cells["B2"].Formula = "= (1+5)*6";

For more information on formulas, refer to the Spreadsheet Formulas section, and review the following examples:

When you set the CellRange.Formula property for a cell range, each cell in this range contains the specified formula with automatically adjusted relative cell references. Therefore, you do not need to duplicate the same formula for each cell and update cell references. Refer to the How to: Create Shared Formulas example for more information.

To perform calculations with arrays of cells, use array formulas. Call the CellRange.ArrayFormula method to create an array formula. Refer to the How to: Create Array Formulas example for more information.

See Also