根据日期更改添加行
Adding Rows based on Date Change
我试图在每次更改日期时在 sheet 中添加行。
因此,例如下面的行:
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
我对此很陌生,并且一直在做很多研究。但是,我无法弄清楚。任何建议都会非常有帮助。提前致谢!
你需要在循环中使用 I not 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
我试图在每次更改日期时在 sheet 中添加行。 因此,例如下面的行:
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
我对此很陌生,并且一直在做很多研究。但是,我无法弄清楚。任何建议都会非常有帮助。提前致谢!
你需要在循环中使用 I not 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