Formula Property

Microsoft Office Web Components Object Model

Formula Property

       

Returns or sets the object's formula in A1-style notation and in the language of the script. If the cell contains a constant, this property returns the constant. If the cell is empty, it returns an empty string. If the cell contains a formula, the Formula property returns the formula as a string in the same format that it would be displayed if the formula were being actively edited (including the equal sign).

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

If the range is a one 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 range fills all cells in the range with the formula. Read/write Variant.

expression.Formula

expression   Required. An expression that returns a Range object.

Example

This example sets the formula, column width, and number format for cell B5 on Sheet1 in Spreadsheet1.

Sub SetFormula()
    Dim rngRandomNum
   
    ' Set a variable to cell B5 on Sheet1.
    Set rngRandomNum = Spreadsheet1.Worksheets("Sheet1").Range("B5")
   
    ' Insert a formula into cell B5.
    rngRandomNum.Formula = "=5*RAND()"
   
    ' Set the number format for cell B5.
    rngRandomNum.NumberFormat = "#.###"
   
    ' Autofit column B.
    rngRandomNum.Columns.AutoFit

End Sub