XL.Sheet.CellWriteFormula
-------------------------

Writes a formula into cell.

| Component | Version | macOS | Windows | Linux | Server | iOS SDK |
|---|---|---|---|---|---|---|
| [XL](component_XL.md) | [3.5](newinversion35.md) | ✅ Yes | ✅ Yes | ✅ Yes | ✅ Yes | ✅ Yes |

MBS( "XL.Sheet.CellWriteFormula"; BookRef; SheetIndex; Row; Column; Formula { ; Format } ) 

**MBS**( **"XL.Sheet.CellWriteFormula";** /\* Writes a formula into cell. \*/  
**$BookRef**; /\* The reference number for the workbook.e.g. $ref \*/   
**$SheetIndex**; /\* The index of the sheet. This number ranges from 0 to [XL.Book.SheetCount](XLBookSheetCount.md)-1.e.g. 0 \*/   
**$Row**; /\* The row for the cell. First row has index 0. \*/   
**$Column**; /\* The column for the cell. First column has index 0. \*/   
**$Formula**; /\* The new formula to write.e.g. "1+2" \*/   
**$Format**) /\* Optional; The format for the cell. This is index from 0 to XL.GetFormatCount-1 in the book's list of formats.e.g. 0 \*/ 

### Parameters

| Parameter | Description | Example | Flags |
|---|---|---|---|
| BookRef | The reference number for the workbook. | $ref |  |
| SheetIndex | The index of the sheet. This number ranges from 0 to [XL.Book.SheetCount](XLBookSheetCount.md)-1. | 0 |  |
| Row | The row for the cell. First row has index 0. | $row |  |
| Column | The column for the cell. First column has index 0. | $column |  |
| Formula | The new formula to write. | "1+2" |  |
| Format | The format for the cell. This is index from 0 to XL.GetFormatCount-1 in the book's list of formats. | 0 | Optional |

### Result

Returns OK or error message.

### Description

Writes a formula into cell.  
As LibXL does not calculate the result of the formula, it will not encode the result in the Excel file. It does only store the formula, so an app opening the file will have to do the calculation. But QuickLook on macOS does not calculate, so results do not show there.  
See also [XL.Sheet.CellReadFormula](XLSheetCellReadFormula.md) and [XL.Sheet.CellIsFormula](XLSheetCellIsFormula.md).  
Examples for formulas are: "TODAY()", "IF(C1&gt;0;ABS(C1\*D1);"""")", "SUM(E16:E38)" or "E39+E39\*E40".  
  
See also [XL.Sheet.CellWriteFormulaBoolean](XLSheetCellWriteFormulaBoolean.md), [XL.Sheet.CellWriteFormulaNumber](XLSheetCellWriteFormulaNumber.md) and [XL.Sheet.CellWriteFormulaText](XLSheetCellWriteFormulaText.md) where you can provide the result of the calculation as most viewer don't calculate and just show the stored value. Excel will perform missing calculations when you open the document.  
### Examples

Loads book, writes some cells and saves back to container field:

 #Load template excel file   
Set Variable \[$bookRef ; Value:MBS ( "[XL.LoadBook](XLLoadBook.md)"; XL::ExcelFile )\]  
Set Variable \[$sheet ; Value:0\]  
\#First row   
Set Variable \[$row ; Value:4\]  
Set Variable \[$r ; Value:MBS ( "[XL.Sheet.CellWriteNumber](XLSheetCellWriteNumber.md)"; $BookRef ; $sheet ; $row ; 0; 1 )\]  
Set Variable \[$r ; Value:MBS ( "[XL.Sheet.CellWriteText](XLSheetCellWriteText.md)"; $BookRef ; $sheet ; $row ; 1; "Cherries" )\]  
Set Variable \[$r ; Value:MBS ( "[XL.Sheet.CellWriteNumber](XLSheetCellWriteNumber.md)"; $BookRef ; $sheet ; $row ; 2; 5 )\]  
\#Second row   
Set Variable \[$row ; Value:5\]  
Set Variable \[$r ; Value:MBS ( "[XL.Sheet.CellWriteNumber](XLSheetCellWriteNumber.md)"; $BookRef ; $sheet ; $row ; 0; 2 )\]  
Set Variable \[$r ; Value:MBS ( "[XL.Sheet.CellWriteText](XLSheetCellWriteText.md)"; $BookRef ; $sheet ; $row ; 1; "Banana" )\]  
Set Variable \[$r ; Value:MBS ( "[XL.Sheet.CellWriteNumber](XLSheetCellWriteNumber.md)"; $BookRef ; $sheet ; $row ; 2; 3,5 )\]  
\#Third row   
Set Variable \[$row ; Value:6\]  
Set Variable \[$r ; Value:MBS ( "[XL.Sheet.CellWriteNumber](XLSheetCellWriteNumber.md)"; $BookRef ; $sheet ; $row ; 0; 3 )\]  
Set Variable \[$r ; Value:MBS ( "[XL.Sheet.CellWriteText](XLSheetCellWriteText.md)"; $BookRef ; $sheet ; $row ; 1; "Oranges" )\]  
Set Variable \[$r ; Value:MBS ( "[XL.Sheet.CellWriteNumber](XLSheetCellWriteNumber.md)"; $BookRef ; $sheet ; $row ; 2; 6,25 )\]  
\#Last row   
Set Variable \[$row ; Value:13\]  
Set Variable \[$r ; Value:MBS ( "XL.Sheet.CellWriteFormula"; $BookRef ; $sheet ; $row ; 2; "SUM(C5:C13)" )\]  
\#Save to container and cleanup   
Set Field \[XL::OutputFile ; MBS ("XL.Save"; $bookRef ; "test.xls")\]  
Set Variable \[$r ; Value:MBS ( "[XL.Book.Release](XLBookRelease.md)"; $bookRef )\]  
Set Field \[XL::Result ; $r \]  
Writes a formula with bold font format:

 # add a bold font based on another font   
Set Variable \[ $textFontBold ; Value: MBS ( "[XL.Book.AddFont](XLBookAddFont.md)"; $book ; $textFont ) \]   
Set Variable \[ $r ; Value: MBS ( "[XL.Font.SetBold](XLFontSetBold.md)"; $book ; $textFontBold ; 1) \]   
\# add format based on other format   
Set Variable \[ $dollarFormatBold ; Value: MBS ( "[XL.Book.AddFormat](XLBookAddFormat.md)"; $book ; $dollarFormat ) \]   
Set Variable \[ $r ; Value: MBS ( "[XL.Format.SetFont](XLFormatSetFont.md)"; $book ; $dollarFormatBold ; $textFontBold ) \]   
\# use format   
Set Variable \[ $r ; Value: MBS ( "XL.Sheet.CellWriteFormula"; $book ; $sheet ; 38; 4; "SUM(E16:E38)"; $dollarFormatBold ) \]  
### See also

- [XL.Sheet.CellReadNumber](XLSheetCellReadNumber.md)
- [XL.Sheet.CellType](XLSheetCellType.md)
- [XL.Sheet.CellWriteComment](XLSheetCellWriteComment.md)
- [XL.Sheet.CellWriteError](XLSheetCellWriteError.md)
- [XL.Sheet.CellWriteFormulaBoolean](XLSheetCellWriteFormulaBoolean.md)
- [XL.Sheet.CellWriteFormulaNumber](XLSheetCellWriteFormulaNumber.md)
- [XL.Sheet.CellWriteFormulas](XLSheetCellWriteFormulas.md)
- [XL.Sheet.CellWriteMatrix](XLSheetCellWriteMatrix.md)
- [XL.Sheet.CellWriteNumber](XLSheetCellWriteNumber.md)
- [XL.Sheet.CellWriteNumbers](XLSheetCellWriteNumbers.md)

### Release notes

- **Version 8.0**
    - Added [XL.Sheet.CellReadBooleans](http://www.mbsplugins.eu/XLSheetCellReadBooleans.shtml), [XL.Sheet.CellReadComments](http://www.mbsplugins.eu/XLSheetCellReadComments.shtml), [XL.Sheet.CellReadDates](http://www.mbsplugins.eu/XLSheetCellReadDates.shtml), [XL.Sheet.CellReadFormulas](http://www.mbsplugins.eu/XLSheetCellReadFormulas.shtml), [XL.Sheet.CellReadNumbers](http://www.mbsplugins.eu/XLSheetCellReadNumbers.shtml), [XL.Sheet.CellReadTexts](http://www.mbsplugins.eu/XLSheetCellReadTexts.shtml), [XL.Sheet.CellWriteBlanks](http://www.mbsplugins.eu/XLSheetCellWriteBlanks.shtml), [XL.Sheet.CellWriteBooleans](http://www.mbsplugins.eu/XLSheetCellWriteBooleans.shtml), [XL.Sheet.CellWriteComments](http://www.mbsplugins.eu/XLSheetCellWriteComments.shtml), [XL.Sheet.CellWriteDates](http://www.mbsplugins.eu/XLSheetCellWriteDates.shtml), [XL.Sheet.CellWriteFormulas](http://www.mbsplugins.eu/XLSheetCellWriteFormulas.shtml), [XL.Sheet.CellWriteNumbers](http://www.mbsplugins.eu/XLSheetCellWriteNumbers.shtml), [XL.Sheet.CellWriteTexts](http://www.mbsplugins.eu/XLSheetCellWriteTexts.shtml), [XL.Sheet.GetCellErrors](http://www.mbsplugins.eu/XLSheetGetCellErrors.shtml), [XL.Sheet.GetCellFormats](http://www.mbsplugins.eu/XLSheetGetCellFormats.shtml), [XL.Sheet.RemoveComments](http://www.mbsplugins.eu/XLSheetRemoveComments.shtml) and [XL.Sheet.SetCellFormats](http://www.mbsplugins.eu/XLSheetSetCellFormats.shtml).

### Example Databases

- [XL/Edit Sheet](https://www.mbsplugins.eu/MBS-FileMaker-Plugin-Examples/XL/Edit%20Sheet.shtml#1ScriptAnchor_)
- [XL/Writing data](https://www.mbsplugins.eu/MBS-FileMaker-Plugin-Examples/XL/Writing%20data.shtml#5ScriptAnchor_)

### Blog Entries

- [MBS Plugin Advent calendar: 13 - XL](https://www.mbsplugins.de/archive/2023-12-13/MBS_Plugin_Advent_calendar_13_/monkeybreadsoftware_blog_filemaker)

This function checks for a license.

Created 18th August 2014 , last changed 29th August 2023

  
[XL.Sheet.CellWriteError](XLSheetCellWriteError.md) - [XL.Sheet.CellWriteFormulaBoolean](XLSheetCellWriteFormulaBoolean.md)

[HTML Version](XLSheetCellWriteFormula.shtml)