Sub topic 9
Calculated columns
A calculated column is a table column where one formula applies to every row. You enter the formula once. Excel fills it down the whole column and keeps it in sync as the table changes. This chapter explains how that works, how to override it, and which errors to expect.
This chapter assumes you can already create a table, name it, and read a basic structured reference such as SalesTable[Amount]. It also assumes you know A1-style references and the difference between relative and absolute addresses.
What a calculated column does
In a plain range, you write a formula in the first row and copy it down. If you add a row later, you must copy the formula again. If you edit one cell, the column can become inconsistent without any warning.
A table handles this for you. When you type a formula into an empty column of a table, Excel treats the column as a calculated column. It does three things:
- It fills the formula into every row of the column.
- It fills the formula into any row you add later.
- It updates every row when you edit the formula in one cell.
Calculated columns depend on two earlier ideas. Structured references let the formula name columns instead of cell addresses. Automatic expansion makes the table add the row, so the formula follows.
Creating a calculated column
Suppose a table named SalesTable has the columns Product, Quantity, Unit Price and Amount. The Amount column is empty. Click the first cell under the Amount header and type this formula:
=[@Quantity]*[@[Unit Price]]Press Enter. Excel fills the formula down the whole column. Each row now multiplies its own Quantity and Unit Price.
Read the parts of the formula this way:
[@Quantity]means the Quantity value in the same row as the formula.@is the "this row" marker. Older workbooks may show[#This Row],[Quantity]instead. Both forms mean the same thing.[@[Unit Price]]needs an extra pair of brackets because the column name contains a space.
You can also build the formula by clicking cells. If you click the Quantity cell in the same row while typing, Excel writes [@Quantity] for you instead of C2. This is the easiest way to avoid typing mistakes in column names.
How the fill-down works
The behavior comes from an Excel option called "Fill formulas in tables to create calculated columns". It is on by default. You can find it under File > Options > Proofing > AutoCorrect Options > AutoFormat As You Type.
The fill happens only when the column is empty. If the column already holds values, typing a formula in one cell changes just that cell. Excel then shows a small AutoCorrect Options button, where you can choose to overwrite all cells with the formula. Clear the column first if you want the automatic fill.
Using other tables and totals in a calculated column
A calculated column can use any formula, not only arithmetic on the same row. Two common patterns are useful.
The first compares a row to the whole column. This formula gives each row's share of the total:
=[@Amount]/SUM([Amount])Here [@Amount] is one row's value. [Amount] with no @ is the entire Amount column. The formula therefore divides each row by the column total. Because the column reference is a whole-column reference, it grows with the table.
The second looks up a value from another table. If a table named PriceList has Product and Price columns, this formula fetches the price for each row:
=XLOOKUP([@Product],PriceList[Product],PriceList[Price],"Not found")The fourth argument shows a clear message when a product is missing, which is better than a bare #N/A error. If your Excel version lacks XLOOKUP, INDEX and MATCH do the same job.
Absolute references and fixed values
Structured references move with the row, which is what you want most of the time. Sometimes a formula needs one fixed value, such as a tax rate stored in a single cell outside the table.
Place the rate in a cell and give it a name, for example TaxRate. Then write:
=[@Amount]*(1+TaxRate)A named cell is clearer than $H$1, and it does not shift if you insert rows above it. If you use $H$1 instead, the reference stays fixed in every row, which also works. A named cell is easier to read and to audit later.
Exceptions: overwriting one cell
You can type a different formula or a constant into one cell of a calculated column. Excel allows it, and that cell becomes an exception. Excel marks it with a small green triangle in the corner, and the cell is flagged as inconsistent with the column formula.
Exceptions have these effects:
- The cell no longer follows the column formula.
- Later edits to the column formula do not change the exception cell.
- Other rows still update as normal.
Exceptions also appear when you copy cells into a calculated column or move cells within it.
To remove an exception, select the cell and use the AutoCorrect Options button to restore the column formula. Or re-enter the column formula in any non-exception cell and choose to overwrite all.
Editing and removing the column formula
To change the formula, edit it in one cell and press Enter. Excel updates every non-exception cell in the column.
To turn a calculated column into plain values, copy the column and paste it back with Paste Special > Values. The numbers stay and the formulas are gone. You do this when you want to freeze results, for example before sending a file where the source data will no longer be available.
To stop Excel from creating calculated columns automatically, turn off the AutoCorrect option from the earlier section. Most users should leave it on.
Common errors and how to read them
| Symptom | Likely cause | Fix |
|---|---|---|
| Formula fills only one cell | The column already held data | Clear the column, then enter the formula again |
| `#NAME?` after typing a column name | The header text does not match exactly | Check spelling and spaces, or click the cell instead of typing |
| `#REF!` after deleting a column | A formula pointed to the deleted column | Edit the formula to use an existing column |
| Green triangle in a cell | The cell is an exception | Restore the column formula from the AutoCorrect button |
| Formula shows `C2` instead of `[@Quantity]` | Option to use table names in formulas is off | Turn on File > Options > Formulas > Use table names in formulas |
| Column does not extend to new row | The new row sits outside the table | Type directly below the last row, or drag the resize handle |
A renamed header deserves special mention. If you rename the Quantity header to Qty, Excel rewrites every formula that refers to it. The formulas keep working. This is an advantage over typed text, but it also means a formula can silently change its meaning if you reuse a header name for different data.
Calculated columns and VBA
When code writes a formula to a table column, assign it to the column's data body range. Excel then treats the column as calculated, the same as typing it.
Sub AddAmountColumn()
Dim tbl As ListObject
Set tbl = ActiveSheet.ListObjects("SalesTable")
tbl.ListColumns("Amount").DataBodyRange.Formula = "=[@Quantity]*[@[Unit Price]]"
End SubThis sets one formula on all data rows at once. Use .Formula, not .FormulaR1C1, when you want structured references in the text. If the table has no data rows, DataBodyRange is Nothing, and the macro stops with an error. Add at least one row first.
Design guidance
- Keep one formula per column. If a column needs different logic by row, use a single formula with
IFrather than many exceptions. - Prefer
[@Column]over cell addresses inside the table. The formulas are shorter and read like sentences. - Put constants such as rates in named cells outside the table, not inside formulas.
- Keep helper columns next to the data they depend on, and give them clear headers.
- Check for exceptions before you total or export a column.
Exercise
A table named OrderTable has columns Qty, Price and Discount (a fraction such as 0.1). Write a calculated column formula for a Net column that gives Qty times Price, reduced by the discount. Then say what happens when you type 0 over the Net value in row 3.
Show answer
Use =[@Qty][@Price](1-[@Discount]). When you overwrite the Net cell in row 3 with 0, that cell becomes an exception. It is marked with a green triangle, and it no longer follows the column formula. Other rows still update when the formula is edited. To fix it, use the AutoCorrect Options button to restore the column formula for that cell.
Exercise
You type =[@Amount]*2 into the first cell of a table column that already contains numbers in every row. Only that one cell changes. Why, and how do you get the formula into the whole column?
Show answer
Excel fills a formula down only when the column is empty. Because the column already held data, the formula went into one cell. Clear the column and re-enter the formula, or use the AutoCorrect Options button to overwrite all cells in the column with the formula.