Arte SheetsLearn. Try. Understand.

Text

Add Leading Spaces & Indent Hierarchy Levels with REPT(" ", ...)

Create a visual hierarchy by prefixing labels with a validated number of spaces. Keep original labels and numeric levels for sorting and lookup.

Indent labels without changing the original source column

Column A holds whole-number levels starting at 1. D2 holds a nonnegative whole-number spaces-per-level setting. Column C contains the display label; column B remains unchanged for lookup keys and export.

Leading spaces are text characters, not cell alignment, row grouping or a structural tree. Use left alignment and a monospaced font if consistent visual spacing matters. Keep source ordering explicitly; indentation does not create parent-child relationships.

The formula rejects missing, negative or fractional settings and bounds the complete output to 32767 characters before calling REPT. Level 0 is rejected rather than silently treated as a root.

Create the display label in C4

Enter the same pattern in C2:C4, adjusting the relative A/B row references.

=IF(COUNT(A4,$D$2)<>2,"Enter numeric level and step",IF(OR(A4<1,A4<>INT(A4),$D$2<0,$D$2<>INT($D$2)),"Use whole-number level >=1 and step >=0",IF((A4-1)*$D$2+LEN(B4)>32767,"Output exceeds 32767 characters",REPT(" ",(A4-1)*$D$2)&B4)))

Count only the spaces added by this formula

For a valid generated label, subtract the original label length from the result length. This preserves any existing leading, trailing or repeated internal spaces in the source label. LEN(text)-LEN(TRIM(text)) is not a general leading-space counter because TRIM also removes trailing spaces and collapses internal runs.

=IF(COUNT(A4,$D$2)<>2,"Check level and step",IF(OR(A4<1,A4<>INT(A4),$D$2<0,$D$2<>INT($D$2)),"Check level and step",IF((A4-1)*$D$2+LEN(B4)>32767,"Check output length",LEN(C4)-LEN(B4))))

Try the example

Use the original values or change them to test your understanding.

Further reading