简体   繁体   English

VBA 代码帮助 - 需要为每个缺失的日期添加一行并从下面的单元格中复制数据

[英]VBA Code Help - Need to add a line for each missing date and copy data from cells below

I have the below code to add a line for each missing date then update column D with the missing date but I also want the new line to copy the data from the cells below for columns A to c and E to L.我有下面的代码为每个缺失的日期添加一行,然后用缺失的日期更新 D 列,但我也希望新行将数据从下面的单元格复制到 A 列到 c 和 E 到 L。

Currently I end up with a worksheet like this目前我最终得到了这样的工作表在此处输入图像描述

The VBA code is: VBA 代码为:

Dim wks As Worksheet
Set wks = Worksheets("NAV_REPORT")

Dim lastRow As Long
lastRow = Range("D2").End(xlDown).Row

For i = lastRow To 2 Step -1
    curcell = wks.Cells(i, 4).Value
    prevcell = wks.Cells(i - 1, 4).Value

    Do Until curcell - 1 <= prevcell
        wks.Rows(i).Insert xlShiftDown

        curcell = wks.Cells(i + 1, 4) - 1
        wks.Cells(i, 4).Value = curcell
    Loop
Next i

any suggestions for updating the above code to fill up from the cells below?关于更新上面的代码以从下面的单元格中填充的任何建议?

Thanks!谢谢!

If you want the inserted row to be identical to the row above, all you need to do is to copy the row above and insert that like follows:如果您希望插入的行与上面的行相同,您需要做的就是复制上面的行并插入,如下所示:

wks.Rows(i).Copy
wks.Rows(i).Insert xlShiftDown

This will insert the exact data on the previous row and the rest of your code will amend the date as necessary.这将在前一行插入准确的数据,您的代码 rest 将根据需要修改日期。

If I understand you correctly...如果我理解正确的话...

Sub test()
dim c as range: dim dif 
Set c = Range("D2")
Do Until c.Value = ""
    dif = DateDiff("d", c.Value, c.Offset(1, 0).Value)
    If dif > 1 Then
        With c.Offset(1, -3)
            .EntireRow.Copy
            Range(.Cells, .Offset(dif - 2, 0)).Insert Shift:=xlDown
        End With
        c.AutoFill Destination:=Range(c, c.Offset(dif - 1, 0)), Type:=xlFillDefault
        Set c = c.Offset(dif, 0)
    Else
        Set c = c.Offset(1, 0)
    End If
Loop
End Sub

The sub assumes that there is no blank cell in between the rows of data in column D. sub 假定 D 列中的数据行之间没有空白单元格。

the new line to copy the data from the cells below for columns A to c and E to L.新行用于从下面的单元格中复制 A 列到 c 列和 E 列到 L 的数据。

the "below" here is the yellow and orange before running the sub.这里的“下面”是运行潜艇之前的黄色和橙色。

在此处输入图像描述 ===> ===> 在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM