• Hi All

    Please note that at the Chandoo.org Forums there is Zero Tolerance to Spam

    Post Spam and you Will Be Deleted as a User

    Hui...

  • When starting a new post, to receive a quicker and more targeted answer, Please include a sample file in the initial post.

pad text cells

crouchsw

Member
Used this for work, thought someone else might be able to...

If you are using a fixed-width font to make a report, and you need to put in a variable number of spaces based on the contents, you can use rept(" ",10-len(A1)), for example. However, if you are using concatenation and vlookup for a large number of values, this can get kind of crazy.


Paste this function into your vba page:

[pre]
Code:
Function PAD(text As Variant, char As String, length As Integer, Optional padright As Boolean = False)
Dim tlength As Integer
Dim returnstring As String
tlength = Len(text)
returnstring = String(length - tlength, char) & text
If padright = True Then
returnstring = text & String(length - tlength, char)
End If
PAD = returnstring
End Function
[/pre]
you can now put =PAD(A1," ",10)

if you want to pad on the right, you can put =PAD(A1," ",10,TRUE)
 
Back
Top