Just wondering, is there any equivalent in VBA to VB .NET’s PadLeft and PadRight methods?
As of right now, whenever I want to take a string and make it a fixed length with leading spaces, I do a For…Next loop based on the string’s length.
For example, I would use the following code to format a string to 8 characters with leading spaces:
intOrdNoLen = Len(strOrdNo)
For i = 1 To (8 - intOrdNoLen) Step 1
strOrdNo = " " & strOrdNo
Next
Is there a way to do this same thing in fewer lines in VBA?
I don’t believe there are any explicit
PADLEFTorPADRIGHTfunctions, but you can use a combination ofSPACEandLEFTorRIGHTto prepend spaces to your string, and then grab the right X number of characters.PADLEFT
If you want a character instead of spaces, you can use
STRINGinstead of space (the example below left-pads with X):PADRIGHT