macros用于复制去年的行,将复制的单元格插入到下一行,然后将年份更改为当前

我有一个很大的数据表(“数据”),在第一列(例如31.12.2013)中有几个年份的行,以及以下列中的各种数据。

我试图制作一个macros,通过列A,并find每个单元格包含31.12.2013,并没有一个单元格包含31.12.2014下面。 对于每次它应该复制整个行,将复制的单元格插入它下面的新行,并将date从2013年12月31日更改为31.12.2014。

由于这是我第一次尝试制作macros,我不知道自己在做什么。 这是我在网上find的一些代码和来自内置录音机的一些代码混合后得到的,希望有人能够做出一些实际上可以做任何事情的东西:

Sub Nyttaarsregnskap() Dim searchSheet As Worksheet Dim currentRow As Long Dim lastRow As Long Set searchSheet = Sheets("Data") Application.ScreenUpdating = False searchSheet.Activate lastRow = searchSheet.Cells(Rows.Count, "A").End(xlUp).Row For currentRow = 1 To lastRow If InStr(LCase(searchSheet.Cells(currentRow, "A")), "12/31/2013") > 0 Then If Not InStr(LCase(searchSheet.Cells(currentRow + 1, "A")), "12/31/2014") > 0 Then searchSheet.Rows(currentRow & ":" & currentRow).Copy Cells(currentRow + 1, "A").Select Selection.Insert Shift:=xlDown Application.CutCopyMode = False Cells(currentRow + 1, "A").FormulaR1C1 = "12/31/2014" lastRow = lastRow + 1 End If End If Next currentRow End Sub 

试试这个:

 Sub Nyttaarsregnskap() Dim searchSheet As Worksheet Dim currentRow As Long Dim lastRow As Long Application.ScreenUpdating = False Set searchSheet = Sheets("Data") With searchSheet lastRow = .Cells(.Rows.Count, "A").End(xlUp).Row For currentRow = lastRow To 1 Step -1 If Format(.Cells(currentRow, "A"), "mm/dd/yyyy") = Format("12/31/2013", "mm/dd/yyyy") Then If Not Format(.Cells(currentRow + 1, "A"), "mm/dd/yyyy") = Format("12/31/2014", "mm/dd/yyyy") Then .Range(currentRow + 1 & ":" & currentRow + 1).Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove .Rows(currentRow & ":" & currentRow).Copy .Cells(currentRow + 1, "A") .Cells(currentRow + 1, "A").FormulaR1C1 = "12/31/2014" End If End If Next currentRow End With Application.CutCopyMode = False Application.ScreenUpdating = True End Sub