Data Cleaning: Pad An ID With Leading Zeros To A Fixed Width

Excel Formulas › Data Cleaning

All versions

Imports often strip leading zeros, leaving IDs of different lengths. REPT repeats a zero as many times as the ID is short of the target width, and the ampersand joins the zeros to the ID, so every value reaches the same length.


Quick formula: Repeat zero for the missing digits, then append the original ID:
=REPT("0",C2-LEN(B2))&B2

The ID 4127 padded to width 8 becomes 00004127.

Functions used (tap for the full reference guide):

The example

Three worked examples. Change any yellow input in the practice workbook and the results recalculate.

ABCDE
1CaseIDWidthPaddedLength
2ID 141278000041278
3ID 29260000926
4ID 33500188003500188

The formula

Here is the formula, built directly from the inputs:

=REPT("0",C2-LEN(B2))&B2 // Padded; Length

How it works

Here is the logic, one piece at a time:

  1. LEN(B2) counts the existing characters.
  2. C2-LEN(B2) is the missing digits.
  3. REPT("0",...) builds the zeros.
  4. &B2 appends the ID.

Keep every number in its own input cell so the formula never needs editing.

Try it: interactive demo

Interactive

Enter the values below.

Variations

Using TEXT

Fixed width of 8.

=TEXT(B2,"00000000")

Trim spaces first

Clean stray spaces.

=REPT("0",C2-LEN(TRIM(B2)))&TRIM(B2)

Pitfalls & errors

If the ID is longer than the width, REPT gets a negative count and returns #VALUE!.

Keep IDs as text so zeros are not stripped again.

Wrap in IFERROR for mixed-length data.

Practice workbook

Frequently asked questions

Why not TEXT?
TEXT works on numbers with a fixed pattern; REPT handles any width.
Does it work on letters?
Yes, text IDs pad the same way.

Stop fighting formulas. Learn them in a day.

This recipe is one of hundreds of real-world formulas we teach. Our Excel Formulas & Functions class covers lookups, logic, text, and dynamic arrays hands-on — live in Dallas–Fort Worth, Houston, Austin, Oklahoma City, Denver, or online.

See the Formulas & Functions Class

Related formulas: Standardize Phone Number Format · Clean Currency Text to a Number · Remove Non-Breaking Spaces (TRIM Won't)

Function references: REPTLEN