Skip to content

REPT

Repeats text a given number of times. Use it for in-cell bars, dividers and padding.

Syntax

=REPT(, )

Text

All versions of Excel.

Try it

Edit the formula in C2 or change the inputs. The sheet recalculates as you type.

C2
A
B
C
1
Label
Value
Divider
2
Totals
48200
--------------------
3
4

C2 =REPT("-",20) returns --------------------

How REPT works

REPT repeats text a number of times. It looks like a trick function, but it covers three practical jobs: drawing small bar charts inside cells, building dividers and templates, and padding text to a fixed width.

The arguments

  • text is what to repeat. It can be one character or a longer string.
  • number_times is how many copies to produce.

=REPT("-",20) returns twenty dashes. =REPT("ab",3) returns ababab.

If number_times has a fraction, REPT drops it, so =REPT("x",2.9) returns xx. A zero returns an empty string, and a negative number returns #VALUE!. The result can't be longer than 32,767 characters, or the formula returns #VALUE! too.

In-cell bar charts

Make the repeat count depend on a value and you have a bar. =REPT("|",B2/10) draws one bar character for every 10 units, so 40 units gives ||||. Fill it down a column and you get a quick visual comparison with no chart. Use a character that looks solid in your font, such as |, █ or ■. Scale the value so the longest bar fits the column. Because REPT drops fractions, round first if you want normal rounding: =REPT("|",ROUND(B2/10,0)).

Padding with leading zeros

To make IDs a fixed length, repeat zeros for however many are missing and join the ID: =REPT("0",8-LEN(A2))&A2 turns 412 into 00000412. This works on IDs stored as text, and it stays correct whatever the ID's length, as long as the ID is not longer than 8 characters. If it is, 8 minus its length goes negative and REPT returns #VALUE!. For numbers, TEXT with a format such as "00000000" is the shorter option.

Masking

REPT creates the mask in a replacement: =REPLACE(A2,1,LEN(A2)-4,REPT("*",LEN(A2)-4)) hides all but the last four characters of an account number, however long it is. See REPLACE.

Layout and dividers

Join a repeated character with a label for headings in a text report: =REPT("=",30). In a cell with Wrap Text on, a repeated CHAR(10) adds blank lines between items.

Repeating a pattern

REPT repeats the whole text. =REPT("-+",5) gives -+-+-+-+-+. REPT has no separator option, so a repeat with commas between the copies means building the text another way, for example with TEXTJOIN.

What REPT returns

REPT always returns text. A repeated "0" such as "000" is not the number 0. Wrap the result in VALUE if you need a number.

Arguments

text
The text to repeat, such as "|", "-" or "0". It can be several characters long.
number_times
How many times to repeat it. It must be zero or more. A fraction is cut down to a whole number, and 0 returns an empty string. A negative number returns #VALUE!.

Examples

Each example is a live sheet. Change the inputs or the formula and watch the result.

Example 1

A divider line

C2 =REPT("-",20)

Result: --------------------

Twenty dashes. Change the second argument to change the length.

C2
A
B
C
1
Label
Value
Divider
2
Totals
48200
--------------------
3
4

C2 =REPT("-",20) returns --------------------

Example 2

An in-cell bar chart

C3 =REPT("|",B3/10)

Result: |||||||

One bar character for every 10 units. The formula in C2 would give four bars, and C4 two.

C3
A
B
C
1
Region
Units
Bar
2
North
40
3
South
70
|||||||
4
East
20
5

C3 =REPT("|",B3/10) returns |||||||

Example 3

Pad an ID with leading zeros

C2 =REPT("0",8-LEN(A2))&A2

Result: 00000412

The ID has 3 characters, so 8 - 3 = 5 zeros are added in front. The result is text.

C2
A
B
C
1
Order no.
Customer
Padded
2
412
Cardigan
00000412
3
4

C2 =REPT("0",8-LEN(A2))&A2 returns 00000412

Example 4

Mask all but the last four characters

C2 =REPT("*",LEN(A2)-4)&RIGHT(A2,4)

Result: ******7712

REPT supplies one asterisk per hidden character, and RIGHT adds the last four. It works for accounts of any length over four.

C2
A
B
C
1
Account
Owner
Masked
2
4021887712
Harbour Supplies
******7712
3
4

C2 =REPT("*",LEN(A2)-4)&RIGHT(A2,4) returns ******7712

Your turn.

Solve it in the sheet. Every answer is checked, with hints if you need them.

Task 1 of 2

In C2, draw a bar of | characters with one | for every 10 units in B2. Fill it down to C5. The units are multiples of 10.

Answer in C2, then it fills down to C5.

C2
A
B
C
D
E
1
Region
Units
Bar
Order no.
Padded
2
North
40
412
3
South
70
25
4
East
20
7081
5
West
50
9
6
7

Common mistakes

  • A fractional number_times is cut down, not rounded. =REPT("|",2.9) gives two bars. Use ROUND if you want normal rounding.
  • A negative number_times returns #VALUE!. In a padding formula such as REPT("0",8-LEN(A2)), an ID longer than 8 characters makes the count negative.
  • The result is text. A padded ID such as "00000412" will not match the number 412 in a lookup.
  • In-cell bars depend on the font. Some characters, such as |, look different between fonts and sizes. Check the result in the font you will use.
  • The result can't exceed 32,767 characters. A very large number_times returns #VALUE!.

Questions people ask

How do I make a bar chart inside cells in Excel?

Use REPT with a bar character and a count based on the value, for example `=REPT("|",B2/10)`. For a graphic version, use conditional formatting data bars instead.

How do I add leading zeros with REPT?

For text IDs, use `=REPT("0",8-LEN(A2))&A2`. For numbers, `=TEXT(A2,"00000000")` is shorter.

How do I repeat text with a separator between each copy?

REPT has no separator option. Put the separator in the text and remove the extra one at the end, or use TEXTJOIN with a sequence of identical items.

What happens if number_times is zero or negative?

Zero returns an empty string. A negative number returns `#VALUE!`.

Is there a limit to what REPT can create?

A cell holds at most 32,767 characters, so a result longer than that returns `#VALUE!`.

Learn REPT in a real sheet.

Tracks take you from the basics to modelling, one graded exercise at a time.