How to mask all but the last 4 digits in Excel
Formula (Excel and Google Sheets)
=REPT("*",LEN(A2)-4)&RIGHT(A2,4)Repeat a mask character for all but the last 4 digits and join the end: =REPT("*",LEN(A2)-4)&RIGHT(A2,4).
- Result on the example
- one result per row in
B2, filled down toB6 - Works in
- Every current version
- Google Sheets
- Same formula
See it work
The formula is already in B2, filled down to B6. Change any input and the result updates, or edit the formula to experiment.
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.
=REPT("*",LEN(A2)-4)&RIGHT(A2,4)Result"************0921"
A2"4417123455660921"The value in A2.
LEN(A2)16LEN: Number of characters.- text
A2"4417123455660921"
- text
LEN(A2)-412The - operator subtracts.
REPT("*",LEN(A2)-4)"************"REPT: Repeats text.- text
"*" - number_times
LEN(A2)-412
- text
RIGHT(A2,4)"0921"RIGHT: Characters from the end of text.- text
A2"4417123455660921" - num_chars
4
- text
REPT("*",LEN(A2)-4)&RIGHT(A2,4)"************0921"The & operator joins as text.
In Google Sheets
The same formula works in Google Sheets: =REPT("*",LEN(A2)-4)&RIGHT(A2,4). We ran it in Google Sheets mode on the same data and it returns the same results.
Other ways to do it
Each one was run on the same example sheet.
| Formula | Result | When to use it |
|---|---|---|
| =LEFT(A2,4)&REPT("*",LEN(A2)-8)&RIGHT(A2,4) | 4417********0921, 5500********0004, 3400*******0009…Different result | 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.
Try it yourself
Same data, empty answer cell. Your formula is graded instantly, then re-checked on changed data, so a typed-in number won't pass.
Mask all but the last 4 digits
B2, replace every digit except the last 4 with an asterisk and fill down to B6.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).
Checked by running on 2026-10-30: every formula and result on this page was calculated by the Spreadsheet Practice formula engine, in both Excel and Google Sheets modes.