# How to extract the last N characters from a cell in Excel

Use =RIGHT(A2,4) to return the last 4 characters, or =RIGHT(A2,$E$1) with the count in a cell.

**Excel:** `=RIGHT(A2,$E$1)` (Every current version)

**Google Sheets:** the same formula works.

## Example

Sample data (row 1 is the header):

| A | B | C | D | E |
| --- | --- | --- | --- | --- |
| Account | Last digits |  | Characters | 4 |
| ACCT-000184421 |  |  |  |  |
| ACCT-000193307 |  |  |  |  |
| ACCT-000201186 |  |  |  |  |
| ACCT-000215590 |  |  |  |  |
| ACCT-000220043 |  |  |  |  |

Formula in B2, filled to B6: `=RIGHT(A2,$E$1)`

Result: 4421; 3307; 1186; 5590; 0043

## How it works

RIGHT counts characters from the end of the text, so it works whatever the length of the rest. The result is text, which keeps the leading zero in "0043".

## Other ways

- `=MID(A2,LEN(A2)-$E$1+1,$E$1)`: The same with MID: start N-1 characters before the end.

## Common mistakes

- Trailing spaces count as characters, so "4421 " returns "421 ". Use `RIGHT(TRIM(A2),4)` on imported data.
- Long account or card numbers typed as numbers lose digits after 15. Store them as text before extracting.

## Questions

**How do I turn the result into a number?**

Wrap it in VALUE: `=VALUE(RIGHT(A2,4))`. Leading zeros are dropped.

**How do I show only the last 4 digits and mask the rest?**

Use `=REPT("*",LEN(A2)-4)&RIGHT(A2,4)`.

Every formula and result on this page was checked by running it in the Spreadsheet Practice formula engine on 2026-10-30.

Source: https://spreadsheetpractice.com/formulas/extract-last-n-characters
