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
Sub macro180623a() Dim i As Integer, j
As Integer |
Execute the above code in the state of the image below

Result:
| 固定リンク
この記事へのコメントは終了しました。

コメント