Work with Cell References
- 11 minutes to read
A cell reference is a set of coordinates that specify the position of a cell or cell range on a worksheet. Use cell references in formulas to obtain and process data contained in the corresponding cells. The results of formulas that use cell references are automatically updated each time the referenced cell values change.
The Spreadsheet Document API supports A1-style references for cells and ranges. You can also address cells and ranges by their zero-based row and column indexes.
A1 Cell Reference Basics
The A1 cell reference is a combination of the column and row headings to which the cell belongs. A column letter is followed by a row number. Column and row headings are displayed at the top and on the left of a worksheet.

The following table contains examples of A1 references to a single cell, and various cell ranges:
| Cell Reference | Description |
|---|---|
| E7 | Refers to a single cell located at the intersection of column E and row 7. |
| A2:B5 | Refers to a range that includes cells from the top-left cell A2 to the bottom-right cell B5. |
| B:B | Refers to a range that includes the entire column B. |
| B:H | Refers to a range that includes the entire columns B through H. |
| 5:5 | Refers to a range that includes the entire row 5. |
| 5:10 | Refers to a range that includes the entire rows 5 through 10. |
When used in a formula, a cell reference can be relative, absolute or mixed, depending on whether it should be automatically adjusted when the formula is copied.
To copy a formula to another cell or range, call the CellRange.CopyFrom(source, options) method.
Relative Cell References
A relative cell reference specifies a cell’s position relative to the cell that contains the formula. When you copy the formula to another cell, the reference changes to preserve this relative position. For example, if you copy the formula from cell C3 to D6, its relative reference to cell B2 changes to C5.

The following code snippet copies a formula with the B2 relative reference from cell C3 to cell D6 and displays the copied formula (=C5) in the console:
using System;
using DevExpress.Docs.Spreadsheet;
Workbook workbook = new Workbook();
Worksheet worksheet = workbook.Worksheets[0];
worksheet.Cells["C3"].Formula = "=B2";
worksheet["D6"].CopyFrom(worksheet["C3"]);
Console.WriteLine(worksheet.Cells["D6"].Formula);
Absolute Cell References
An absolute cell reference in a formula always refers to a specific cell, and it does not change if the formula is copied or moved to another cell. In absolute cell references, the column letter and row number are preceded by the ‘$’ sign. For example, if you copy the formula from cell C3 to D6, its absolute reference to cell B2 remains unchanged ($B$2).

The following code snippet copies a formula with the $B$2 absolute reference from cell C3 to cell D6 and displays the copied formula (=$B$2) in the console:
using System;
using DevExpress.Docs.Spreadsheet;
Workbook workbook = new Workbook();
Worksheet worksheet = workbook.Worksheets[0];
worksheet.Cells["C3"].Formula = "=$B$2";
worksheet["D6"].CopyFrom(worksheet["C3"]);
Console.WriteLine(worksheet.Cells["D6"].Formula);
Mixed Cell References
A mixed cell reference in a formula combines either an absolute column reference and a relative row reference (for example, $B2), or a relative column reference and an absolute row reference (for example, A$1). If the formula is copied, the absolute element of the mixed reference (the column letter or row number preceded by the ‘$’ sign) remains unchanged, and the relative element of the reference is automatically adjusted.
For example, if you copy the formula from cell B2 to D6, its mixed reference $C3 changes to $C7.

The following code snippet copies a formula with the $C3 mixed reference from cell B2 to cell D6 and displays the copied formula (=$C7) in the console:
using System;
using DevExpress.Docs.Spreadsheet;
Workbook workbook = new Workbook();
Worksheet worksheet = workbook.Worksheets[0];
worksheet.Cells["B2"].Formula = "=$C3";
worksheet["D6"].CopyFrom(worksheet["B2"]);
Console.WriteLine(worksheet.Cells["D6"].Formula);
Use A1-Style References
Pass a cell reference to the Worksheet.Cells indexer or pass a range reference to the Worksheet indexer:
using DevExpress.Docs.Spreadsheet;
using (Workbook workbook = new Workbook()) {
Worksheet worksheet = workbook.Worksheets[0];
// Assign a value to a cell identified by an A1-style reference.
worksheet.Cells["B3"].Value = 125;
// Assign a value to every cell in an A1-style range.
worksheet["D2:F4"].Value = "Text";
}
An invalid cell or range reference causes the corresponding indexer to throw an ArgumentException.
Use Zero-Based Indexes
Specify a cell’s row index first and column index second. Call Worksheet.GetRange and pass the starting row and column indexes followed by the row and column counts.
using DevExpress.Docs.Spreadsheet;
using (Workbook workbook = new Workbook()) {
Worksheet worksheet = workbook.Worksheets[0];
// Assign a value to the cell in row 2 and column 1.
worksheet.Cells[2, 1].Value = 125;
// Assign a value to a range that starts in row 1 and column 3.
worksheet.GetRange(1, 3, 3, 3).Value = "Text";
}
Use the R1C1 Reference Style in Formulas
Set the Workbook.UseR1C1ReferenceStyle property to true to get and assign formulas in the R1C1 reference style. In this style, a reference specifies the row number after the letter R and the column number after the letter C (for example, R2C3 refers to cell C2). A relative reference specifies row and column offsets in square brackets (for example, R[-1]C[-1]).
Cell and range references that you pass to the Worksheet.Cells indexer and other API members use the A1 reference style regardless of the UseR1C1ReferenceStyle property value.
The following code snippet enables the R1C1 reference style, displays a formula in this style, and assigns an R1C1-style formula to cell B2:
using System;
using System.IO;
using DevExpress.Docs.Spreadsheet;
Directory.CreateDirectory("output");
using Workbook workbook = new Workbook();
Worksheet worksheet = workbook.Worksheets[0];
worksheet.Cells["A1"].Value = 10;
worksheet.Cells["A2"].Value = 20;
worksheet.Cells["B1"].Formula = "=A1*2";
// Switch formulas to the R1C1 reference style.
workbook.UseR1C1ReferenceStyle = true;
Console.WriteLine(worksheet.Cells["B1"].Formula);
worksheet.Cells["B2"].Formula = "=RC[-1]*2";
workbook.Calculate();
using FileStream stream = File.Create(Path.Combine("output", "Document.xlsx"));
workbook.Save(stream);
Cross-Worksheet References
To refer to a cell or cell range on another worksheet, specify the worksheet name and an exclamation point (!) before the cell reference (for example, Sheet2!C3).
Enclose the worksheet name in single quotation marks (‘) if the name meets one of the following conditions:
- The name contains a character other than a letter, digit, or underscore (for example, a space or parentheses).
- The name starts with a digit.
- The name matches an A1 cell reference.
- The name starts with an R1C1 cell reference.
When you rename a worksheet with the Sheet.Name property, the API updates the worksheet name in all formulas that refer to this worksheet. The API also adds quotation marks if the new name requires them.
The following code snippet assigns a formula to cell B2 that subtracts a value on the Q1 Costs worksheet from a value on the Sales worksheet:
using DevExpress.Docs.Spreadsheet;
Workbook workbook = new Workbook();
Worksheet summary = workbook.Worksheets[0];
Worksheet sales = new Worksheet("Sales");
Worksheet costs = new Worksheet("Q1 Costs");
workbook.Worksheets.Add(sales);
workbook.Worksheets.Add(costs);
sales.Cells["C3"].Value = 1500;
costs.Cells["C5"].Value = 900;
summary.Cells["B2"].Formula = "=Sales!C3-'Q1 Costs'!C5";
To get a cell range by a reference that includes a worksheet name, call the Workbook.GetRange(reference) or Workbook.TryGetRange(reference, range) method. Worksheet.GetRange methods and the Worksheet indexer do not accept worksheet names.
The following code snippet gets a cell range on the Q1 Costs worksheet by a reference that includes the worksheet name and checks a reference to a worksheet that the workbook does not contain:
using System;
using DevExpress.Docs.Spreadsheet;
using Workbook workbook = new Workbook();
Worksheet costs = new Worksheet("Q1 Costs");
workbook.Worksheets.Add(costs);
// Enclose a worksheet name that contains a space in single quotes.
CellRange range = workbook.GetRange("'Q1 Costs'!C5:C7");
Console.WriteLine(range.ToString(CellReferenceOptions.IncludeSheetName));
if (!workbook.TryGetRange("Sheet5!A1", out CellRange missingRange))
Console.WriteLine("The workbook does not contain the Sheet5 worksheet.");
3D References
A 3D reference refers to the same cell or cell range on multiple worksheets. To create a 3D reference, specify the first and last worksheet names separated by a colon, an exclamation point (!), and the cell or range reference (for example, Sheet2:Sheet5!B2:C3).
A 3D reference includes all worksheets between the first and last worksheets in the workbook sheet order. If you insert a worksheet between these worksheets, the reference includes cells on the inserted worksheet. If you remove the first or last worksheet in the range, the API adjusts the reference to the remaining worksheets (for example, Jan:Mar!B2 becomes Feb:Mar!B2 when you remove the Jan worksheet).
The following image shows a formula that sums values in cells B2:C3 on worksheets from Sheet2 to Sheet5:

The following code snippet creates the Jan, Feb, and Mar worksheets and adds a formula to cell B5 that sums B2 cell values on these worksheets:
using DevExpress.Docs.Spreadsheet;
Workbook workbook = new Workbook();
Worksheet summary = workbook.Worksheets[0];
Worksheet january = new Worksheet("Jan");
Worksheet february = new Worksheet("Feb");
Worksheet march = new Worksheet("Mar");
workbook.Worksheets.Add(january);
workbook.Worksheets.Add(february);
workbook.Worksheets.Add(march);
january.Cells["B2"].Value = 1200;
february.Cells["B2"].Value = 1350;
march.Cells["B2"].Value = 1100;
summary.Cells["B5"].Formula = "=SUM(Jan:Mar!B2)";
Structured References
Structured references refer to tables and table parts by table and column names instead of cell addresses. A structured reference has the following syntax:
TableName[[#Data],[ColumnName]]
TableNameis the table name (Table.Name property).[#Data]is an item specifier that refers to the data range of a table or table column. Other item specifiers are[#All],[#Headers],[#Totals], and[#This Row].[ColumnName]is the column name (TableColumn.Name property). Without an item specifier, a column specifier refers to the column data range excluding header and total cells.
Use the following operators to combine column specifiers:
- Colon (
:) - a range of adjacent columns (for example,Sales[[Q1]:[Q3]]). - Comma (
,) - a union of columns (for example,(Sales[Q1],Sales[Q3])). - Space - an intersection of references.
When you rename a table or table column, the API updates structured references in all formulas that refer to this table or column.
The following code snippet creates a Sales table from cells A1:B3 and adds a formula to cell D1 that sums values in the Amount table column:
using DevExpress.Docs.Spreadsheet;
Workbook workbook = new Workbook();
Worksheet worksheet = workbook.Worksheets[0];
worksheet.Cells["A1"].Value = "Product";
worksheet.Cells["B1"].Value = "Amount";
worksheet.Cells["A2"].Value = "Chai";
worksheet.Cells["B2"].Value = 180;
worksheet.Cells["A3"].Value = "Chang";
worksheet.Cells["B3"].Value = 270;
Table table = new Table(0, 0, 3, 2, "Sales");
worksheet.Tables.Add(table, true);
worksheet.Cells["D1"].Formula = "=SUM(Sales[Amount])";
The following image shows structured references with different item specifiers and operators in a workbook opened in Microsoft Excel:
