在EXCEL中插入缺失的行和值

时间:2016-01-10 07:41:22

标签: excel

我有下表:

FOREIGN KEY (city_id) REFERENCES public.cities (id)
MATCH SIMPLE ON UPDATE NO ACTION ON DELETE NO ACTION

通过减去Date Value Date Diff Hours Diff 29/12/2014 8:00 24.940 29/12/2014 9:00 24.960 0.04 1 29/12/2014 11:00 25.020 0.08 2 29/12/2014 12:00 25.070 0.05 1 列(Date DifF)中的行值来计算列Date=A2-A1的计算方法是将Hours Diff乘以24(Date DifF)。 如果=C2*24的值大于1,我想在右边插入额外的小时,这样每小时就会有一行。 例如,第三行添加了缺失值Hours Diff(2014年12月29日10:00):

Date

之后,我想填写并更正列的值:Date Value Date Diff Hours Diff 29/12/2014 8:00 24.940 29/12/2014 9:00 24.960 0.04 1 29/12/2014 10:00 1 29/12/2014 11:00 25.020 0.08 1 29/12/2014 12:00 25.070 0.05 1 ValueDate Diff等于值的增量除以新行数+ 1(ValueB4-B2/2等于 (25.020 - 24.960)/2。 新表将是:

0.03

1 个答案:

答案 0 :(得分:1)

当您插入或删除行时,请务必记住您需要从底部开始并向顶部工作。如果从上到下进行尝试,您将不可避免地跳过行或循环遍历已经覆盖的行。

Sub insertHourly()
    Dim rw As Long, hr As Long, hrs As Long

    With Worksheets("Sheet2")
        For rw = (.Cells(Rows.Count, 1).End(xlUp).Row - 1) To 2 Step -1
            hrs = Round((.Cells(rw + 1, 1).Value2 - .Cells(rw, 1).Value2) * 24, 0)
            For hr = 2 To hrs
                .Cells(rw + 1, 1).EntireRow.Insert
                .Cells(rw + 1, 1) = .Cells(rw + 2, 1) - TimeSerial(1, 0, 0)
                .Cells(rw + 1, 4) = 1
            Next hr
        Next rw
    End With
End Sub

insert_hours
在insertHourly程序之前

insert_hours_results
在insertHourly程序之后