個人的スクリプト垂れ流し
VBAで文字列編集するときに作ったモジュールを置いとく。
どうせまたいつか必要になるだろうし。
Attribute VB_Name = "StringUtility" Option Explicit Option Compare Binary ''' ----------------------------------------------------------------------------- ''' <summary> ''' 文字列を左詰めし、指定された桁まで文字を付加して返却します ''' </summary> ''' <param name="value">文字列</param> ''' <param name="padChar">付加する文字列</param> ''' <param name="length">文字列付加後の桁数</param> ''' <returns>編集された文字列</returns> ''' <remarks> ''' 特になし ''' </remarks> ''' <history> ''' [foohogehoge] 2008/04/23 Created ''' </history> ''' ----------------------------------------------------------------------------- Public Function padLeft(ByVal value As String, ByVal padChar As String, ByVal length As Integer) As String While (asciiLenB(value) < length) value = value & padChar Wend padLeft = value End Function ''' ----------------------------------------------------------------------------- ''' <summary> ''' 文字列を右詰めし、指定された桁まで文字を付加して返却します ''' </summary> ''' <param name="value">文字列</param> ''' <param name="padChar">付加する文字列</param> ''' <param name="length">文字列付加後の桁数</param> ''' <returns>編集された文字列</returns> ''' <remarks> ''' 特になし ''' </remarks> ''' <history> ''' [foohogehoge] 2008/12/04 Created ''' </history> ''' ----------------------------------------------------------------------------- Public Function padRight(ByVal value As String, ByVal padChar As String, ByVal length As Integer) As String Dim padStr As String padStr = "" While (asciiLenB(padStr) < length - asciiLenB(value)) padStr = padStr & padChar Wend padRight = padStr & value End Function ''' ----------------------------------------------------------------------------- ''' <summary> ''' 現在の文字列長から、最大長までに必要な数のタブ文字を返却します ''' </summary> ''' <param name="currentLen">現在の文字列長</param> ''' <param name="maxLen">文字列の最大長</param> ''' <param name="tabWidth">タブ幅</param> ''' <returns>タブ文字</returns> ''' <remarks> ''' 特になし ''' </remarks> ''' <history> ''' [foohogehoge] 2008/04/23 Created ''' </history> ''' ----------------------------------------------------------------------------- Public Function getTabify(ByVal currentLen As Integer, ByVal maxLen As Integer, ByVal tabWidth As Integer) As String Dim tabcnt As Integer Dim max_tab As Integer Dim cur_tab As Integer Dim i As Integer max_tab = maxLen \ tabWidth If (maxLen Mod tabWidth) > 0 Then max_tab = max_tab + 1 End If cur_tab = currentLen \ tabWidth tabcnt = max_tab - cur_tab getTabify = getTab(tabcnt) End Function ''' ----------------------------------------------------------------------------- ''' <summary> ''' 指定した数のタブ文字を返却します ''' </summary> ''' <param name="length">タブ文字の数</param> ''' <returns>指定した個数のタブ文字</returns> ''' <remarks> ''' 特になし ''' </remarks> ''' <history> ''' [foohogehoge] 2008/04/23 Created ''' </history> ''' ----------------------------------------------------------------------------- Public Function getTab(length As Integer) As String Dim tabStr As String Dim i As Integer For i = 1 To length tabStr = tabStr & vbTab Next getTab = tabStr End Function ''' ----------------------------------------------------------------------------- ''' <summary> ''' ASCIIバイト長取得 ''' </summary> ''' <param name="strValue">文字列</param> ''' <returns>ASCIIバイト長</returns> ''' <remarks> ''' 特になし ''' </remarks> ''' <history> ''' [foohogehoge] 2008/04/24 Created ''' </history> ''' ----------------------------------------------------------------------------- Public Function asciiLenB(ByVal strValue As String) As Integer asciiLenB = LenB(StrConv(strValue, vbFromUnicode)) End Function