FormulaCraft

How to mask part of a string in Excel and Google Sheets

Topic:Text manipulation
Excel & Google Sheets
=LEFT(A2,2)&REPT("*",LEN(A2)-4)&RIGHT(A2,2)

Verified example

Computed by a real spreadsheet engine on the sample data below.

Sensitive
AB123456
XY789012
PQ456789
LM321654

=LEFT(A2,2)&REPT("*",LEN(A2)-4)&RIGHT(A2,2)AB****56

Try it with your data

Edit the grid or formula, then run it through a real spreadsheet engine — no signup.

Sample data — click any cell to edit

Runs server-side · free · no signup

Step by step

  1. 1Decide how many characters to reveal at the start and end — a common pattern is 2 visible on each side.
  2. 2Use LEFT(A2,2) to get the first two characters.
  3. 3Use REPT("*",LEN(A2)-4) to generate asterisks for every character between the visible portions.
  4. 4Use RIGHT(A2,2) for the last two characters.
  5. 5Concatenate all three parts: =LEFT(A2,2)&REPT("*",LEN(A2)-4)&RIGHT(A2,2).

Tips

Working on a sheet you inherited? Run the Auditor on the whole file first — it flags every #REF!, #N/A, broken column pattern, and inconsistent formula in seconds, free, no signup.

Frequently asked

What if the string is shorter than the combined visible characters?

REPT with a negative number returns an empty string, so the formula degrades gracefully. Add an IF to handle very short strings: =IF(LEN(A2)>4, LEFT(A2,2)&REPT("*",LEN(A2)-4)&RIGHT(A2,2), REPT("*",LEN(A2))).

Can I mask an email address?

Yes — extract the username and domain separately with FIND("@",A2) and mask only the username portion before reassembling.

More on Text manipulation

See all →

Formulas used

Related tasks

Written and reviewed by FormulaCraft Team. Each formula on this page is run through our verification engine before publishing.

Last reviewed: