【发布时间】:2018-04-07 13:08:26
【问题描述】:
下面的代码很好,唯一的问题是处理时间太长。 有谁知道如何加快速度?
Public Sub separate_line_break()
target_col = "H" 'Define the column you want to break
delimiter = Chr(10) 'Define your delimiter if it is not space
ColLastRow = Range(target_col & Rows.Count).End(xlUp).Row
Application.ScreenUpdating = False
For Each Rng In Range(target_col & "1" & ":" & target_col & ColLastRow)
If InStr(Rng.Value, delimiter) Then
Rng.EntireRow.Copy
Rng.EntireRow.Insert
Rng.Offset(-1, 0) = Mid(Rng.Value, 1, InStr(Rng.Value, delimiter) - 1)
Rng.Value = Mid(Rng.Value, Len(Rng.Offset(-1, 0).Value) + 2, Len(Rng.Value))
End If
Next
ColLastRow2 = Range(target_col & Rows.Count).End(xlUp).Row
For Each Rng2 In Range(target_col & "1" & ":" & target_col & ColLastRow2)
If Len(Rng2) = 0 Then
Rng2.EntireRow.Delete
End If
Next
Application.ScreenUpdating = True
End Sub
我想要实现的是根据行拆分 F 列中的数据。你可以看到有些行包含不止一行(数字)。
我想将每行编号超过 1 的每一行分隔成一个新行,如第二张图片所示。
有人可以请教。非常感谢您的帮助。谢谢
拆分后的数据:
拆分前的数据:
【问题讨论】:
-
您可以将整个范围读入一个变体数组 (
Arr=rng.value2),在数组中执行所有操作,然后一步将其写回。这应该更快(我敢打赌 100 倍左右)。如果您遇到问题,请回来。 -
您好 Jochen,我不是程序员,如果您能帮我修改代码,不胜感激。我在网上找到了代码并自己修改了它,但完全不知道如何实现您的建议。
标签: excel excel-formula vba