让Excel为多列的缺少日期插入新行

时间:2016-06-15 16:04:24

标签: excel vba excel-vba

所以我有一个大约18列日期的工作簿,其中包含数据点和大约3k行。我试图确保所有行都有相同的日期。如果缺少日期,我想要插入一个缺少日期的新行和旁边的#N / A.与之前的专栏相比,有些专栏已经跳过了几天。

我已经搜索了,我想我找到了一个可能有效的宏,但是我对VBA知之甚少,所以我不确定如何调整它它适用于多列:

sum(case when num % 2 = 0 then num else 0 end) over last_2
  

编辑:我已附加图片以便更轻松!

这显示日期未对齐的问题

Cells that are missing in row

这显示固定行 - 单元格向下移动

http://i.stack.imgur.com/Uhg9D.png

理想情况下,我希望能够找到找到所有缺失日期的方法并插入这样的行,而无需手动执行。

1 个答案:

答案 0 :(得分:0)

这个怎么样:

Option Explicit

Sub insertMissingDate()
    Const FIRST_ROW     As Integer = 2

    Const COMPARE_COL   As Integer = 3
    Const COL1          As String = "C"
    Const COL2          As String = "D"

    Dim wks             As Worksheet
    Dim strRange        As String
    Dim lastRow         As Long
    Dim i               As Long

    Dim curCell         As Date
    Dim prevCell        As Date

    Set wks = Worksheets("Sheet1")


    lastRow = wks.Range(COL1 & FIRST_ROW).End(xlDown).Row

    'Work bottom up since we are inserting new rows
    For i = FIRST_ROW To lastRow

        curCell = wks.Cells(i, COMPARE_COL).Value
        prevCell = wks.Cells(i, COMPARE_COL - 2).Value

        If curCell <> prevCell Then
            strRange = COL1 & i & ":" & COL2 & i
            Range(strRange).Insert Shift:=xlDown

            wks.Cells(i, COMPARE_COL).Value = prevCell
            wks.Cells(i, COMPARE_COL + 1).Value = "#N/A"
        End If
    Next i

    Set wks = Nothing
End Sub