其实没那么难。
首先,将所有的日志注释存储在一个数组中
第二,循环遍历数组中的每个日志注释,并且对于每个循环,执行一个 do while 循环以插入新行;将值复制到新行;每 60 个字符分割字符串。
只要子串的长度超过60就继续循环。
第三,用于H列单元格的拆分;这可以通过使用 Mid 函数根据字符位置显示值来完成。
例如,第一行我们将从字符位置 1 到 60 显示
例如。 Mid(subString, 1, 60)
而对于第二行,我们将从字符位置 61 开始显示
例如。 Mid(subString, 61, Len(subString))
然后对于后续的 do while 循环,第二行将从字符位置 1 到 60 等显示。
Mid 功能信息:
中间(字符串,开始,长度)
string = 完整的原文
start = 字符的起始位置
长度 = 字符长度
单元格偏移信息:
.Cells(rowNo, colNo).Offset(RowOffset, ColumnOffset)
RowOffset = 例如。 [1 = 单元格下方 1 行] [-1 = 单元格上方 1 行]
ColumnOffset = 例如。 [1 = 单元格右侧 1 列] [-1 = 单元格左侧 1 列]
完整解决方案
Option Explicit
'split every 60 characters new row
Sub SplitEverySixtyChar()
Dim ws As Worksheet
Dim rowNo As Long
Dim lastRowNo As Long
Dim arrayLogNote As Variant
Dim logNote As Variant
Dim subString As String
Set ws = ThisWorkbook.Sheets("Sheet1")
With ws
'Find last row no
lastRowNo = (.Cells(.Rows.Count, 1).End(xlUp).Row)
'Store all log note in array
arrayLogNote = .Range("H2:H" & lastRowNo).Value
'Starting row no is 2
rowNo = 2
For Each logNote In arrayLogNote
subString = CStr(logNote)
Do While Len(subString) > 60
'Insert new row
.Cells(rowNo, 1).Offset(1, 0).EntireRow.Insert
'Copy the cell from A:G col into new row
.Cells(rowNo, 1).Offset(1, 0).Value = .Cells(rowNo, 1).Value
.Cells(rowNo, 2).Offset(1, 0).Value = .Cells(rowNo, 2).Value
.Cells(rowNo, 3).Offset(1, 0).Value = .Cells(rowNo, 3).Value
.Cells(rowNo, 4).Offset(1, 0).Value = .Cells(rowNo, 4).Value
.Cells(rowNo, 5).Offset(1, 0).Value = .Cells(rowNo, 5).Value
.Cells(rowNo, 6).Offset(1, 0).Value = .Cells(rowNo, 6).Value
.Cells(rowNo, 7).Offset(1, 0).Value = .Cells(rowNo, 7).Value
'Display text for new row from character position 60 onwards
.Cells(rowNo, 8).Offset(1, 0).Value = Mid(subString, 61, Len(subString))
'Display text from character position 1 to 60
.Cells(rowNo, 8).Value = Mid(subString, 1, 60)
subString = .Cells(rowNo, 8).Offset(1, 0).Value
'Increment Row No
rowNo = rowNo + 1
Loop
'Increment Row No
rowNo = rowNo + 1
Next logNote
End With
End Sub
注意:这可能不是目前最有效的解决方案。肯定有比这更好的解决方案。