插入行,如果还没有

我需要在新数据之间插入一个分隔符行,并忽略已经分离的上面的现有数据。

这段代码是这样做的,但是每次运行时都会将行添加到空行中。

Sub InsertRows() Dim lngLstRow As Long lngLstRow = Range("u" & Rows.Count).End(xlUp).Row For i = lngLstRow To 11 Step -1 If Not IsEmpty(Range("u" & i)) Then If Range("u" & i) <> Range("u" & i - 1) Then Range("u" & i).EntireRow.Insert End If End If Next End Sub 

如果此行或上一行为空,则需要将代码更改为不插入额外的行:

 Sub InsertRows() Dim lngLstRow As Long Dim i As Long lngLstRow = Range("u" & Rows.Count).End(xlUp).Row For i = lngLstRow To 11 Step -1 If Not (IsEmpty(Range("u" & i)) Or IsEmpty(Range("u" & i - 1))) Then If Range("u" & i) <> Range("u" & i - 1) Then Range("u" & i).EntireRow.Insert End If End If Next End Sub