Using if statement to insert spaces

kac1125

Board Regular
Joined
Jul 31, 2014
Messages
71
Office Version
  1. 365
Platform
  1. Windows
I am trying to figure a formula to insert spaces depending on the number of digits in a given cell. For example A1 has a 6, I need B2 to be a cell that has 6 spaces in it so I can then do A1&B2 to show as 0000006 (0's indicate spaces) Also, fore example if A1 was 12 I would need B2 to be a cell that shows 5 spaces so I can then do A1&B2 to show 0000012 (again 0's indicate spaces). SO in summary Column be would be cells with only spaces. Please let me know if this makes sense and if you have any ideas for me.

Thanks!
 

Excel Facts

Copy PDF to Excel
Select data in PDF. Paste to Microsoft Word. Copy from Word and paste to Excel.
Code:
Function addSpace(rng As Range) As String

    For x = 1 To 6 - Len(rng.value)
        addSpace = addSpace & " "
    Next x
    
    addSpace = addSpace & rng.value


End Function

Here's a custom function to do that if you don't want an ugly nested if statement. You could even modify it (to the below code) to be variable with the number of spaces.

Code:
Function addSpace(rng As Range, length As Long) As String

    For x = 1 To length - Len(rng.value)
        addSpace = addSpace & " "
    Next x
    
    addSpace = addSpace & rng.value


End Function
 
Upvote 0
I don't think that you need column B to contain only spaces. How about

=REPT(" ",7-LEN(A1))&A1
 
Upvote 0
Or this, put 7 spaces between the quotes:

Code:
=RIGHT("       "&A1,7)
 
Upvote 0
Thanks everyone for responding! VoG yours was exactly what I was looking for! Thanks again! I really appreciate your help, saves me loads of time!
 
Upvote 0

Forum statistics

Threads
1,214,377
Messages
6,119,185
Members
448,872
Latest member
lcaw

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top