Any method equivalent to PadLeft/PadRight?

前端 未结 7 1933
春和景丽
春和景丽 2020-12-03 09:23

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 leng

7条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-03 10:10

    You could use these. Put them in a public module

    'NB Fails if input string is longer than the total length

    Function PadLeft(text As Variant, totalLength As Integer, padCharacter As String) As String
        PadLeft = String(totalLength - Len(CStr(text)), padCharacter) & CStr(text)
    End Function
    
    Function PadRight(text As Variant, totalLength As Integer, padCharacter As String) As String
        PadRight = CStr(text) & String(totalLength - Len(CStr(text)), padCharacter)
    End Function
    

提交回复
热议问题