我试图在每次更改日期时在工作表中添加行。因此,例如下面的行:
1/1/2020
1/1/2020
2/2/2020
2/2/2020
2/2/2020
3/3/2020
3/3/2020
3/3/2020
这是我尝试过的代码,
Public Sub InsertRow()
Dim lastRow, chkRw, I As Integer
lastRow = Cells(Rows.Count, 1).End(xlUp).Row
chkRw = lastRow - 1
For I = lastRow To 2 Step -1
If Cells(lastRow, 1) <> Cells(chkRw, 1) Then
Cells(lastRow, 1).EntireRow.Insert shift:=xlDown
End If
Next
End Sub
我对此并不陌生,已经做了很多研究。但是,我无法弄清楚。任何建议将超级有帮助。预先感谢!
您需要在循环内使用我而不是lastRow
Public Sub InsertRow()
With ActiveSheet
Dim lastRow As Long
lastRow = .Cells(.Rows.Count, 1).End(xlUp).Row
Dim I As Long
For I = lastRow To 2 Step -1
If .Cells(I, 1) <> .Cells(I - 1, 1) Then
.Rows(I).Insert shift:=xlDown
End If
Next
End With
End Sub