« 文字列を任意の文字数以下になるようコンマで改行する | トップページ | セルに入力した文字をはみ出して表示させる »

2018年6月30日 (土)

Break a character string with a comma to make it less than or equal to the specified number of characters

I wrote a method for breaking a character string in a cell in the following two articles.
Break a character string in a cell
Break a character string in a cell with a comma

This time in a way that combined methods of these two articles,
Make a line feed with a comma so that one line is less than 10 characters.

Since it may be difficult to understand the code slightly than the articles,v I will explain the following macro 180609a.

Store the character string you want to add a line break in "Str1".
"StrNum" specifies the maximum number of characters per line.
We will store a character string that adds a newline to "Str2".

① Stores the first to tenth characters of "Str1" in "Str3".
② In this "Str 3"
it is judged whether each character is equal to "," or ",".
I will examine it from the back of the string.
A character string from the first character up to the character with the comma and a line feed
Add it to "Str2".
③ Change "Str1" to the character string after the comma determined by "Str3".
④ Using this modified "Str1"
The first to tenth characters of "Str1" are stored in "Str3".
Below is the repetition of ② to ④.

Finally, if the character string of "Str1" is smaller than 11 characters
And there is no comma in "Str 3"
Add "Str 3" to "Str 2" without adding a line feed.

Also, when it is not the last line
If there is no comma in "Str 3"
Add "Str 3" and line feed to "Str 2".

Code here

Code that breaks a character string with a comma so that it is less than the desired number of characters::

Sub macro180623a()
'Break a character string
'Within 10 characters
'Separate by half-width characters, full-width comma

    Dim i As Integer, j As Integer
    Dim StrNum As Integer
    Dim StrLen As Integer
    Dim c As Object
    Dim Str1 As String, Str2 As String, Str3 As String
    Dim Rng As Range
   
    StrNum = 10 'word count
   
    Set Rng = ActiveSheet.Range("A1:A1") 'Range
   
    For Each c In Rng
        Str1 = c.Text
        StrLen = Len(Str1)
        Str2 = ""
        For i = 1 To StrLen
            Str3 = Mid(Str1, 1, 10)
            For j = Len(Str3) To 1 Step -1
                If Mid(Str3, j, 1) = "," Or Mid (Str3, j, 1) = "," Then
                    Str2 = Str2 & Mid(Str3, 1, j) & Chr(10)
                     Str1 = Mid(Str1, j + 1, Len(Str1))
                    j = 1
                Else
                    If j = 1 Then
                       'Last line
                        If Len(Str1) < StrNum + 1 Then
                            Str2 = Str2 + Str3
                            i = StrLen + 1
                         Else
                         'Without commnas in not the last line
                            Str2 = Str2 + Str3 & Chr(10)
                            Str1 = Mid(Str1, 11, Len(Str1))
                        End If
                    End If
                End If
            Next j
        Next i
        Cells (c.Row, c.Column) = Str2
    Next c
   
End Sub

Execute the above code in the state of the image below
Vba20180623a
Result:
Vba20180623b

|

« 文字列を任意の文字数以下になるようコンマで改行する | トップページ | セルに入力した文字をはみ出して表示させる »

コメント

この記事へのコメントは終了しました。

トラックバック


この記事へのトラックバック一覧です: Break a character string with a comma to make it less than or equal to the specified number of characters:

« 文字列を任意の文字数以下になるようコンマで改行する | トップページ | セルに入力した文字をはみ出して表示させる »