« セル内の文字列をコンマで改行する | トップページ | 文字列を任意の文字数以下になるようコンマで改行する »

2018年6月17日 (日)

Break a character string in a cell with a comma

In the previous article "Break a character string in a cell"
I introduced a code that breaks every five characters.
In this article, I will try on a method of breaking a line with a comma.
Since there may be more than one comma in the string,
From the first character to the last character string
I will judge with an If statement
Whether or not each character is equal to ","

If the character is equal to ","
We will add Chr (10) to the string as in the previous article.

Code here

A code for breaking a character string with a single byte comma:

Sub macro180609a()
'Break a string
'Break by commas

    Dim i As Integer
    Dim c As Object
    Dim Str As String
    Dim Rng As Range
   
    Set Rng = ActiveSheet.Range("A1:A2") 'Range
   
    For Each c In Rng
        Str = ""
        For i = 1 To Len(c.Text)
            If Mid(c, i, 1) = "," Then
                Str = Str & Mid(c, i, 1) & Chr(10)
             Else
                Str = Str & Mid(c, i, 1)
            End If
        Next i
        Cells(c.Row, c.Column) = Str
    Next c
   
End Sub

Execute the above code in the state of the image below
Vba20180609a
Result:
Vba20180609b

Here are parts that were not broken by execution results.
The comma which did not break is full size when it sees well.
In the above code, it will only break at a half-size comma.

To pick up both half-size and full size commas
Use the "Or" operator for the condition of the If statement as follows.

c = "," Or c = ","

It returns True when c is either a half-size comma or a full-width comma.

Here is the code under this condition

A code for breaking a character string by both a half-size comma and a full-size comma:

Sub macro180609b()
'Break a string
'Break by half-size and full-width commas

    Dim i As Integer
    Dim c As Object
    Dim Str As String
    Dim Rng As Range
   
    Set Rng = ActiveSheet.Range("A1:A2") 'Range
   
    For Each c In Rng
        Str = ""
        For i = 1 To Len(c.Text)
            If Mid (c, i, 1) = "," Or Mid(c, i, 1) = "," Then
                 Str = Str & Mid(c, i, 1) & Chr(10)
            Else
                Str = Str & Mid(c, i, 1)
            End If
        Next i
        Cells(c.Row, c.Column) = Str
    Next c
   
End Sub

Execute the above code in the state of the image below
Vba20180609a
Result:
Vba20180609c

A line feed was made with a comma in both half size and full size.
You can use more than one "Or" operator side by side.
By specifying the condition of the If statement as follows,
The type of letter you want to break is increased.

c = "," Or c = "," Or c = "、"

Whether it is VBA or worksheet function,
There are many things that it will not work
because of differences between half size and full size.
It is troublesome.

|

« セル内の文字列をコンマで改行する | トップページ | 文字列を任意の文字数以下になるようコンマで改行する »

コメント

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

トラックバック


この記事へのトラックバック一覧です: Break a character string in a cell with a comma:

« セル内の文字列をコンマで改行する | トップページ | 文字列を任意の文字数以下になるようコンマで改行する »