# How to mask all but the last 4 digits in Excel

Repeat a mask character for all but the last 4 digits and join the end: =REPT("*",LEN(A2)-4)&RIGHT(A2,4).

**Excel:** `=REPT("*",LEN(A2)-4)&RIGHT(A2,4)` (Every current version)

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

## Example

Sample data (row 1 is the header):

| A | B |
| --- | --- |
| Card number (text) | Masked |
| 4417123455660921 |  |
| 5500000000000004 |  |
| 340000000000009 |  |
| 6011000990139424 |  |
| 4111111111111111 |  |

Formula in B2, filled to B6: `=REPT("*",LEN(A2)-4)&RIGHT(A2,4)`

Result: ************0921; ************0004; ***********0009; ************9424; ************1111

## How it works

REPT repeats the asterisk once for every digit that should be hidden, `LEN(A2)-4` of them, and RIGHT keeps the last 4. Because the count comes from the length, a 15-digit card gets 11 asterisks and a 16-digit card 12.

## Other ways

- `=LEFT(A2,4)&REPT("*",LEN(A2)-8)&RIGHT(A2,4)`: Shows the first 4 and last 4 digits, a common format for receipts. ("4417********0921".)

## Common mistakes

- Card numbers typed as numbers lose their last digit: Excel keeps only 15 significant digits, so 4417123455660921 is stored as 4417123455660920. Store them as text.
- Masking in a formula doesn't hide the original. Delete the source column, or paste the masked values into a new file before sharing.

## Questions

**How do I mask an email address?**

Keep the first letter and the domain: `=LEFT(A2,1)&"***"&MID(A2,FIND("@",A2),LEN(A2))`.

**How do I group the mask in fours, like **** **** **** 0921?**

For 16 digits, join a fixed mask: `="**** **** **** "&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/mask-all-but-last-4-digits
