Sub topic 2
Your First Formula: Starting with =
A formula is an instruction stored in a cell. Excel calculates it and shows the result in the cell. This chapter covers how to enter, view, edit and cancel a formula, and how to fix it when Excel treats your formula as plain text.
This chapter assumes you can open a workbook, select a cell by clicking it or using the arrow keys, and type a value and press Enter. It also assumes you know cell addresses such as B2, meaning column B and row 2. The operators + - * / and the order in which Excel applies them have their own chapters. Here the formulas are kept simple so the entry mechanics stay in focus.
What a formula is
Every cell holds one of two kinds of content.
- A constant is something you typed that Excel stores as it is: a number such as
42, a date, or text such asRent. - A formula is an expression that Excel evaluates. The cell shows the result, but Excel keeps the expression.
This gives each formula cell two views of the same cell. The cell shows the result. The formula bar, the wide box above the column letters, shows what is stored. Select a cell containing =5+3 and the cell shows 8 while the formula bar shows =5+3.
A constant never changes unless you retype it. A formula is recalculated whenever something it depends on changes. That is the reason to use formulas.
The equals sign is the signal
Excel decides what you are typing from the first character. If the first character is =, Excel parses everything after it as a formula. Otherwise it treats the entry as a constant, as a number, date or text.
=5+3Type that into a cell and press Enter. The cell shows 8.
What happens without it
Type 5+3 with no equals sign and press Enter. The cell shows 5+3, left-aligned, which is how Excel displays text. Excel did not make a mistake. You gave it text, so it stored text.
This is the most common first-day error, and the left alignment is the clue. Numbers align to the right by default, so a "calculation" that sits at the left edge of the cell was probably never a formula.
Other starting characters
Excel also treats an entry starting with + or - as a formula, for compatibility with older spreadsheet programs. Typing -5+3 gives -2, and Excel stores it as =-5+3. Start with = anyway. It is unambiguous, and it works the same everywhere.
Text inside formulas
A formula can return text. The text must be in double quotes.
="Hello"The cell shows Hello. Without the quotes, Excel reads Hello as a name it should look up and finds nothing. The result is a #NAME? error, covered in a later chapter.
Entering a formula step by step
Start with a blank sheet and these two constants.
| Cell | Content you type | Cell shows |
|---|---|---|
| A1 | 12 | 12 |
| A2 | 30 | 30 |
Then enter a formula in A3.
- Click
A3. - Type
=A1+A2. - Press Enter.
=A1+A2A3 shows 42. Select A3 again and the formula bar shows =A1+A2. Now change A1 to 20. A3 immediately shows 50. You did not touch the formula. Excel noticed that a cell it depends on changed and recalculated.
You can also enter the same formula by pointing, which avoids typing addresses:
- Click
A3and type=. - Click
A1. Excel insertsA1into the formula. - Type
+. - Click
A2. - Press Enter.
Pointing is faster and avoids typos in addresses. While you are typing, each referenced cell gets a colored border that matches the color of its address in the formula. Use those colors to check that you picked the cells you meant.
Committing and cancelling an entry
While you type, the content exists only in an editing buffer. Nothing is stored until you commit it. These keys end the entry in different ways.
| Key | Effect |
|---|---|
| Enter | Commits and moves the selection down one cell |
| Tab | Commits and moves the selection right one cell |
| Shift+Enter | Commits and moves the selection up one cell |
| Ctrl+Enter | Commits and keeps the same cell selected |
| Esc | Cancels the entry and restores the previous content |
| Clicking the check mark in the formula bar | Commits and keeps the same cell selected |
The direction Enter moves is a setting (File > Options > Advanced > "After pressing Enter, move selection"). If Enter on your machine moves somewhere else, someone changed that option.
Ctrl+Enter is useful when you want to commit a formula and inspect its result without losing your place.
Esc is your safety net. If you start editing an existing formula and realize you are wrecking it, press Esc before Enter and the cell returns to what it was. After you press Enter, only Undo (Ctrl+Z) can bring it back.
The cell, the formula bar and what Excel does
The following diagram shows what Excel does with an entry from the moment you commit it.
Note the branch for invalid syntax. Type =5+ and press Enter, and Excel refuses to commit. It shows a dialog saying there is a problem with the formula and leaves you in the cell to fix it. A formula that is well formed but cannot be calculated, such as dividing by zero, is a different case. Excel commits it and shows an error value in the cell. You will meet those in a later chapter.
Enter, Edit and Point modes
The status bar at the bottom left of the window shows what Excel thinks you are doing. For formulas, three modes matter.
- Ready: nothing is being typed. Arrow keys move the selection.
- Enter: you are typing into the cell. If you have typed
=or an operator, arrow keys pick cells instead of moving the text cursor. Excel's term for that is Point mode, and the status bar can showPointwhile you are choosing a cell. - Edit: you are changing existing content. Arrow keys move the text cursor inside the entry.
F2 switches between Enter and Edit while you type. This catches people out. You type =A1+, press the right arrow expecting to move the cursor, and the formula changes to =A1+B1. The arrow key was in Point mode and selected a cell. Press F2 to switch to Edit mode, and the arrows move the cursor.
Constants and references
You can enter both of these in a cell.
=12+30=A1+A2Both return 42 today. They differ when the data changes. The first has the numbers written into it. To change one, you must edit the formula. The second reads the numbers from A1 and A2, so changing either cell updates the result.
Typing numbers into a formula is called hard-coding. It is fine for a one-off check or a fixed value such as the number of months in a year. For data that can change, such as prices, quantities and rates, put the values in cells and refer to them. The chapter on cell references develops this. For now, the habit to build is that formulas with data in them should point at cells.
Viewing and editing formulas
To change an existing formula, use any of these:
- Select the cell and press F2. The cursor goes to the end of the formula, in Edit mode.
- Double-click the cell. The cursor goes where you clicked.
- Select the cell and click in the formula bar. This is helpful for long formulas, because the bar is wider than most cells.
Then change the text and press Enter to commit it, or Esc to discard your changes.
To see every formula on the sheet at once, press Ctrl+` (the grave accent key, usually at the top left of the keyboard) or choose Formulas > Show Formulas. Cells now show their formulas instead of their results, and the columns widen so the text fits. Press the same shortcut again to switch back. This is a quick way to audit a sheet when you did not build it.
If a formula is long, the formula bar can be expanded with the arrow at its right end or with Ctrl+Shift+U. A formula can be up to 8,192 characters long, which is more than you will use in this tutorial.
You can add spaces for readability. =A1 + A2 and =A1+A2 give the same result, because Excel ignores spaces around operators. Excel does not care about capitalization of cell addresses either: =a1+a2 is accepted and stored as =A1+A2.
Troubleshooting entry problems
When a formula "does not work" in your first week, the cause is usually one of the items below. Check them in order.
| What you see | Likely cause | Fix |
|---|---|---|
| The cell shows `=A1+A2` as text instead of a result | The cell is formatted as Text | Set Home > Number Format to General, then press F2 and Enter to re-enter the formula |
| The cell shows `'=A1+A2` in the bar, or the text sits left-aligned | A leading apostrophe forced text | Select the cell, press F2, delete the apostrophe, press Enter |
| Every formula on the sheet shows as text | Show Formulas is on | Press Ctrl+` or turn off Formulas > Show Formulas |
| The result does not change when you change an input | Calculation is set to Manual | Formulas > Calculation Options > Automatic, or press F9 to calculate once |
| The cell shows `#####` | The column is too narrow for the result | Widen the column or double-click the right edge of its header |
| A dialog says there is a problem with the formula | Incomplete or unbalanced syntax, such as `=5+` | Complete the formula, then press Enter again |
| The cell shows `#NAME?` | Text without quotes, or a misspelled name | Put literal text in double quotes, and check spelling |
A few of these need more explanation.
Text format. Excel remembers the format of a cell. If a cell is formatted as Text, whatever you type there, including a formula, is stored as text. Changing the format afterward does not fix existing entries. You must re-enter the content, which F2 followed by Enter does. This is common in cells copied or imported from other systems.
Manual calculation. Manual mode is a deliberate setting for very large workbooks, but it surprises people when it gets switched on by an opened file. The status bar shows Calculate when results are out of date. Calculation mode is an application-wide setting that Excel takes from the first workbook opened in a session, which is how a file you open can change it for your other workbooks.
Hash marks. ##### is not a formula error. The formula is fine. Excel cannot fit a number into the cell width and shows hashes instead of cutting digits off. Widening the column shows the value.
A short practice sequence
Try this on a blank sheet to confirm each point above.
- In
B1type100and inB2type25. - In
B3enter=B1-B2by pointing. It should show75. - In
B4type=B1-B2but press Esc instead of Enter.B4stays empty. - Press Ctrl+` and compare the two views, then press it again.
- Change
B2to40. Check thatB3now shows60. - In
C1type1+1and press Enter. Note the left alignment. Press F2, put=at the start, and press Enter. It now shows2.
Step 6 shows the main point of the chapter. The only difference between an entry that calculates and one that does not is the first character.
Exercise
You type 87 into cell D1 and press Enter. The cell shows 87, aligned to the left. What went wrong, and what is the fastest way to fix it without retyping the whole entry?
Show answer
Excel stored the entry as text because it did not start with an equals sign. Select D1, press F2 to enter Edit mode, press Home to move to the start, type =, and press Enter. The cell then shows 56 and the formula bar shows =8*7.
Exercise
Cell C3 contains the formula =A3+B3 and shows 15. You select C3, type 20, and press Enter. Later you change A3 to 100. What does C3 show, and why? How could you have changed the displayed value without losing the formula?
Show answer
C3 still shows 20. Typing 20 replaced the formula with a constant, so C3 no longer depends on A3 or B3. To change the result while keeping the formula, change the input cells A3 or B3, or edit the formula itself with F2. If you replaced it by mistake, Ctrl+Z restores the formula right after the change.
Exercise
A colleague sends you a sheet where every formula cell shows its formula text, such as =B2*C2, instead of a number. List two possible causes and the check for each.
Show answer
First, Show Formulas may be on. Check whether Formulas > Show Formulas is highlighted, or press Ctrl+` to toggle it. Second, the cells may be formatted as Text or have a leading apostrophe. Check Home > Number Format, change it to General, and re-enter each formula with F2 and Enter. If only some cells are affected, a Text format or apostrophe on those cells is the more likely cause, because Show Formulas affects the whole sheet.