我有下表:
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
和Value
。 Date Diff
等于值的增量除以新行数+ 1(Value
)B4-B2/2
等于
(25.020 - 24.960)/2
。
新表将是:
0.03
答案 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