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:
- How to: Use Constants and Calculation Operators in Formulas
- How to: Use Cell and Worksheet References in Formulas
- How to: Use Names in Formulas
- How to: Use Functions and Nested Functions in Formulas
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.