# Creating and Using External Variable

Learn how to create and utilize external variables in spreadsheets, including their evaluation and usage within formulas.

## Content

Spread for WinForms provides support for creating and using external variables in spreadsheets.
An external variable refers to a logical cell defined by a symbol. The name of an external variable is case-sensitive. External variables don't possess a cell context and are evaluated distinctly than a normal cell.

* If a reference in the formula for the external variable is missing a reference to the worksheet, then that reference will refer to the active sheet.
* Formulas for external variables are always evaluated using absolute references. If the formula for the external reference uses relative references, then those references will be converted to absolute references when evaluating the formula.
* The external variable formula can result in a range reference when it is evaluated; in this case, the external variable can be used in an array formula. If such a formula is evaluated for a single cell, which requires a primitive value result, then the value in the top-left cell in the range is used as the context cell, and that cell's value will be returned as the evaluated result.
* The external variable can specify a class instance which inherits from ExternalVariable, which enables the application to implement custom logic to bind external objects with the calculation engine. For a detailed example showing how to inherit from ExternalVariable, refer to [Using External Variables with TextBox Control](/spreadnet/docs/latest/online-win/overview/spwin-devguide/spwin-cell-formula/spwin-external-variable/UsingExternalVariableswithTextBoxControl).

The **AddExternalVariable** method of the [INames](/spreadnet/api/latest/online-win/GrapeCity.Spreadsheet/GrapeCity.Spreadsheet.INames.html) interface can be used to add an external variable to the workbook. While using this method, users need to provide the name of the external variable and the formula or the value of the variable.

## Using Code

Define the external variable using the **AddExternalVariable** method for the workbook.

## Example

The following example code creates two external variables "x" and "y" and uses them within the formulas defined in a particular worksheet of the workbook.

```csharp
// Add a new sheet to your workbook.
FarPoint.Win.Spread.SheetView newsheet = new FarPoint.Win.Spread.SheetView();
fpSpread1.Sheets.Add(newsheet);
fpSpread1.AllowUserFormulas = true;

// Set the values of A1 cell and A2 cell of the active sheet.
fpSpread1.ActiveSheet.Cells[0, 0].Value = "Hello";
fpSpread1.ActiveSheet.Cells[1, 0].Value = "World!";

// Add external variables to the workbook that refer to the value of the cells.
fpSpread1.AsWorkbook().Names.AddExternalVariable("x", "Sheet1!A1");
fpSpread1.AsWorkbook().Names.AddExternalVariable("y", "Sheet1!A2");

// Use the external variables in different cell.
fpSpread1.AsWorkbook().Worksheets[1].Cells["B1"].Formula = "\"The concat name is \" & x & y";
```

```vbnet
Add a new sheet to your workbook.
Dim newsheet As FarPoint.Win.Spread.SheetView = New FarPoint.Win.Spread.SheetView()
fpSpread1.Sheets.Add(newsheet)
fpSpread1.AllowUserFormulas = true

' Set the values of A1 cell and A2 cell of the active sheet.
fpSpread1.ActiveSheet.Cells(0, 0).Value = "Hello"
fpSpread1.ActiveSheet.Cells(1, 0).Value = "World!"

' Add external variables to the workbook that refer to the value of the cells.
fpSpread1.AsWorkbook().Names.AddExternalVariable("x", "Sheet1!A1")
fpSpread1.AsWorkbook().Names.AddExternalVariable("y", "Sheet1!A2")

' Use the external variables in different cell.
fpSpread1.AsWorkbook().Worksheets(1).Cells("B1").Formula = """The concat name is "" & x & y"
```

## See Also

[Formulas in Cells](/spreadnet/docs/latest/online-win/overview/spwin-devguide/spwin-cell-formula)
[Placing a Formula in Cells](/spreadnet/docs/latest/online-win/overview/spwin-devguide/spwin-cell-formula/spwin-formulaplace)
[Specifying a Cell Reference in a Formula](/spreadnet/docs/latest/online-win/overview/spwin-devguide/spwin-cell-formula/spwin-formulacellref)
[Specifying a Sheet Reference in a Formula](/spreadnet/docs/latest/online-win/overview/spwin-devguide/spwin-cell-formula/spwin-formulasheetref)
[Specifying an External Reference in a Formula](/spreadnet/docs/latest/online-win/overview/spwin-devguide/spwin-cell-formula/spwin-externalref)
[Using a Circular Reference in a Formula](/spreadnet/docs/latest/online-win/overview/spwin-devguide/spwin-cell-formula/spwin-formulacircref)
[Nesting Functions in a Formula](/spreadnet/docs/latest/online-win/overview/spwin-devguide/spwin-cell-formula/spwin-formulanested)
[Recalculating and Updating Formulas Automatically](/spreadnet/docs/latest/online-win/overview/spwin-devguide/spwin-cell-formula/spwin-formularecalc)
[Finding a Value Using GoalSeek](/spreadnet/docs/latest/online-win/overview/spwin-devguide/spwin-cell-formula/spwin-formula-goalseek)
[Allowing the User to Enter Formulas](/spreadnet/docs/latest/online-win/overview/spwin-devguide/spwin-cell-formula/spwin-formulaallowuser)
[Creating and Using a Custom Name](/spreadnet/docs/latest/online-win/overview/spwin-devguide/spwin-cell-formula/spwin-formulacustomname)
[Creating and Using a Custom Function](/spreadnet/docs/latest/online-win/overview/spwin-devguide/spwin-cell-formula/spwin-formulacustomfunc)
[Using the Array Formula](/spreadnet/docs/latest/online-win/overview/spwin-devguide/spwin-cell-formula/spwin-arrayformula)
[Working with the Formula Text Box](/spreadnet/docs/latest/online-win/overview/spwin-devguide/spwin-cell-formula/spwin-formulabar)
[Setting up the Name Box](/spreadnet/docs/latest/online-win/overview/spwin-devguide/spwin-cell-formula/spwin-namebox)
[Using Language Package](/spreadnet/docs/latest/online-win/overview/spwin-devguide/spwin-cell-formula/UsingLanguagePackage)
[Accessing Data from Header or Footer](/spreadnet/docs/latest/online-win/overview/spwin-devguide/spwin-cell-formula/spwin-headerfooterformula)
[Managing External Reference](/spreadnet/docs/latest/online-win/overview/spwin-devguide/spwin-cell-formula/spwin-extrenalreference)
[Working With Dynamic Array Formulas](/spreadnet/docs/latest/online-win/overview/spwin-devguide/spwin-cell-formula/WorkingWithDynamicArrayFormulas)