有没有一种方法可以自动在Microsoft Excel的现有单元格之间插入新单元格(带有日期值)。例如:
-Date-
1/1/2018
1/2/2018
1/5/2018
我想在1/2/2018至1/5/2018之间插入2个新单元格(1/3/2018和1/4/2018)
答案 0 :(得分:0)
假设您的日期在A列中,以下VBA代码将遍历您的列,检查日期值是否已增加一个月(如果没有),然后插入新行并在序列中添加下一个日期,直到您将到达专栏的末尾:
Sub Add_Dates()
Dim ws As Worksheet: Set ws = ThisWorkbook.Worksheets("Sheet1")
'declare and set your worksheet, amend as required
i = 2
Do Until ws.Cells(i + 1, 1).Value = ""
'check next rows value
NextDate = DateAdd("m", 1, ws.Cells(i, 1).Value)
'add one month to the date
If ws.Cells(i + 1, 1).Value <> NextDate Then
'if next date is not what we expect
ws.Cells(i + 1, 1).Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
'insert row
ws.Cells(i + 1, 1).Value = NextDate
'add new date
End If
i = i + 1
'increment
Loop
End Sub