[英]How to insert new row when cell value below does not equal to active cell value
How to insert new row when cell value below does not equal to active cell value当下面的单元格值不等于活动单元格值时如何插入新行
simply how to get this result:简单地说如何得到这个结果:
currently the vba code is:目前 vba 代码为:
Sub InsertBlankRowsBasedOnCellValue()
Dim Col As Variant
Dim BlankRows As Long
Dim LastRow As Long
Dim R As Long
Dim StartRow As Long
Col = "A"
StartRow = 1
BlankRows = 1
LastRow = Cells(Rows.Count, Col).End(xlUp).Row
Application.ScreenUpdating = False
With ActiveSheet
For R = LastRow To StartRow + 1 Step -1
If .Cells(R, Col) <> LastRow Then
.Cells(R, Col).EntireRow.Insert Shift:=xlDown
End If
Next R
End With
Application.ScreenUpdating = True
End Sub
The condition条件
If .Cells(R, Col) <> LastRow Then
mixes cell values and row numbers oddly.奇怪地混合单元格值和行号。
There you should just check if you are at the last low.在那里你应该检查你是否处于最后一个低点。 Then you can't compare it to anything yet, use the following condition to skip that case:
那么你还不能将它与任何东西进行比较,使用以下条件来跳过这种情况:
If R <> LastRow Then
For all the other rows you should compare the two consecutive cell values对于所有其他行,您应该比较两个连续的单元格值
If .Cells(R, Col).Value <> .Cells(R + 1, Col).Value Then
So here is the whole code:所以这里是整个代码:
Dim Col As Variant
Dim LastRow As Long
Dim R As Long
Dim StartRow As Long
Col = "A"
StartRow = 1
LastRow = Cells(Rows.Count, Col).End(xlUp).Row
With ActiveSheet
Application.ScreenUpdating = False
For R = LastRow To StartRow + 1 Step -1
If R <> LastRow Then
If .Cells(R, Col).Value <> .Cells(R + 1, Col).Value Then
.Cells(R + 1, Col).EntireRow.Insert Shift:=xlDown
End If
End If
Next R
End With
Application.ScreenUpdating = True
End Sub结束子
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.