我对这个答案采取了不同的方法。它不是最简单的,但可能是最通用的。我使用了 LittleBobbyTables 和 Brad 的一些代码来制作这个。以下是有关如何使用该功能的几个示例:
Sub test()
Debug.Print PadStr("ABC", 6) 'returns "ABC "
Debug.Print PadStr("ABC", 6, "-") 'returns "ABC---"
Debug.Print PadStr("ABC", 6, , xlHAlignRight) 'returns " ABC"
Debug.Print PadStr("ABC", 7, "*", xlHAlignCenter) 'returns "**ABC**"
Debug.Print PadStr("ABC", 9, "*", xlHAlignDistributed) 'returns "**A**B*C*"
Debug.Print PadStr("ABC", 7, "*", xlHAlignFill) 'returns "ABCABCA"
End Sub
这是功能:
Function PadStr(Expression As Variant, length As Integer, Optional padChar As String = " ", Optional alignment As XlHAlign = xlHAlignGeneral) As String
'Pads a string with a given character.
'@Expression - the string to pad
'@length - the minimum length of the string (if @Expression is longer than @length, the original Expression will be returned)
'@padChar - the character to pad with (a space by default)
'@alignment - what type of alignment to use. Uses the XlAlign object for enumeration.
' xlHAlignLeft - (Default) Aligns input text to the left
' xlHAlignGeneral - Same as Default
' xlHAlignRight - Aligns input text to the right
' xlHAlignCenter - Center aligns text
' xlHAlignCenterAcrossSelection - Same as xlHAlignCenter
' xlHAlignDistributed - Distributes the text evenly within the length specified
' xlHAlignJustify - Same as xlHAlignDistributed
' xlHAlignFill - Fills the specified length with the text
'example: if input is "ABC", " ", "8", see code below for what the output will be given the different direction options
If Len(Expression) >= length Or (padChar = "" And alignment <> xlHAlignFill) Then
'if input is longer than pad-length padChar or no input given for padChar (note: padChar doesn't matter when
'using xlHAlignFill) just return the input
PadStr = Expression
ElseIf Len(padChar) <> 1 And alignment <> xlHAlignFill Then
'give error if padChar is not exactly 1 char in length (again, padChar doesn't matter when using xlHAlignFill)
'padChar must be 1 char long because string() only accepts 1 char long input.
Err.Raise vbObjectError + 513, , "input:'padChar' must have length 1." & vbNewLine & "SUB:PadStr"
Else
Dim pStr As String, i As Long
Select Case alignment
Case xlHAlignLeft, xlHAlignGeneral '(Default)
'"ABC "
PadStr = CStr(Expression) & String(length - Len(CStr(Expression)), padChar)
Case xlHAlignRight
'" ABC"
PadStr = String(length - Len(CStr(Expression)), padChar) & CStr(Expression)
Case xlHAlignCenter, xlHAlignCenterAcrossSelection
'" ABC "
pStr = String(Application.WorksheetFunction.RoundUp((length / 2) - (Len(Expression) / 2), 0), padChar)
PadStr = pStr & Expression & pStr
Case xlHAlignDistributed, xlHAlignJustify
'" A B C " (" A B C " if lenth=9)
Dim insPos As Long, loopCntr As Long: loopCntr = 1
PadStr = Expression
Do While Len(PadStr) < length
For i = 1 To Len(Expression)
PadStr = Left(PadStr, insPos) & padChar & Right(PadStr, Len(PadStr) - insPos)
insPos = insPos + 1 + loopCntr
If Len(PadStr) >= length Then Exit For
Next i
PadStr = PadStr & padChar
loopCntr = loopCntr + 1
insPos = 0
Loop
Case xlHAlignFill
'"ABCABCAB"
For i = 1 To Application.WorksheetFunction.RoundUp(length / Len(Expression), 0)
PadStr = PadStr & Expression
Next i
Case Else
'error
Err.Raise vbObjectError + 513, , "PadStr does not support the direction input ( " & direction & ")." & vbNewLine & "SUB:PadStr"
End Select
PadStr = Left(PadStr, length) 'output cannot be longer than the given length
End If
End Function