23

Is it possible to pad a cell with spaces to ensure that it is the correct length?

Here's an example with * representing spaces.

Input    Output
-----    ------
red      red**
blue     blue*

The words are padded with spaces to ensure the cell content is 5 characters long.

Jean-François Corbett
  • 37,420
  • 30
  • 139
  • 188
Terry
  • 363
  • 1
  • 2
  • 7

3 Answers3

35

Try this:

=LEFT(A1&"*****",5)

We are adding lots of stars(*) then just cutting from left 5 characters.

enter image description here

zx8754
  • 52,746
  • 12
  • 114
  • 209
  • 4
    This method can be reversed to provide leading zeroes to special numbers destined to be exported to a database. Some part numbers, serial numbers and in particular DUNS numbers must be padded with leading zeroes to a fixed length. `=RIGHT("000000000" & A1, 9)` will produce the correct result. –  Mar 31 '15 at 08:44
  • 2
    @Jeeped Yes, this is how I would pad numbers to have the same length. Another option would be `=TEXT(A1,"0000")` see http://stackoverflow.com/a/3992588/680068 – zx8754 Mar 31 '15 at 08:48
22

As per suggested comment:

=LEFT(A1 & REPT("*",5),5)

original:

=A1 & REPT("*",5-len(A1))

main advantage being that you can pass the length and the pad character as a cell reference, and easily update

user3616725
  • 3,485
  • 1
  • 18
  • 27
0

I had to right pad the numbers in a cell with 0 to make it maximum 9 characters. All had uneven numbers in the cell. I used the below formula to achieve the result. For me the least characters present in the cell was 3. =LEFT(G41 & REPT("0",6),9)

  • 1
    This might be on the right track, but doesn't really answer the question. The OP shows strings padded with spaces to a length of 5, but you have padded with `0` to a length of 9. – MattClarke Mar 03 '17 at 04:22