如何在excel中自动添加“符合如下条件的”数据行
C D E
5 50601930 2010-02-22 08:54 28.20
6 50601930 2010-02-22 09:00 28.21
7 50601930 2010-02-22 09:48 28.20
8 50601930 2010-02-22 10:00 28.21
9 50601930 2010-02-22 10:06 28.20
10 50601930 2010-02-22 10:24 28.21
11 50601930 2010-02-22 10:30 28.20
12 50601930 2010-02-22 10:36 28.21
13 50601930 2010-02-22 10:54 28.22
14 50601930 2010-02-22 11:12 28.21
15 50601930 2010-02-22 11:42 28.22
16 50601930 2010-02-22 11:54 28.23
17 50601930 2010-02-22 12:06 28.24
18 50601930 2010-02-22 12:12 28.23
上面的数据是从excel表中摘得。
我想根据D栏的时间进行自动插入数据行,要求插入的数据行的时间为整点,插入行的E栏采用的数据为整点之前“最靠近整点的那个时刻”对应的E栏的数据。C栏的数据不变。D栏的日期若正好在时间为00:00时,自动转换成下一天的日期。
比如:
13 50601930 2010-02-22 10:54 28.22
14 50601930 2010-02-22 11:12 28.21
时间没有2010-02-22 11:00这个整点时刻的数据,我想要在13和14行中间自动插入
“14 50601930 2010-02-22 11:00 28.22”这些数据,并单独作为一行,原本的第14行自动下移成为第15行。
让原来的13和14行自动转化为13、14和15行,如下所示:
13 50601930 2010-02-22 10:54 28.22
14 50601930 2010-02-22 11:00 28.22
15 50601930 2010-02-22 11:12 28.21
其中,14行的50601930 2010-02-22 与28.22采用的都是第13行的数据。
如何能根据D栏的时间,自动插入数据行????
迫切等待解决方法,本人在此感谢各位高手了,谢谢!!!
[解决办法]
'vbaSub Macro1() Dim i As Long, j As Long Dim x1, x2, x3: i = 1 Do While Cells(i + 1, 4) <> "" '第四列是日期列 x1 = Split(Split(Cells(i, 4))(1), ":")(0) x2 = Split(Split(Cells(i + 1, 4))(1), ":")(0) x3 = Split(Split(Cells(i + 1, 4))(1), ":")(1) If x1 <> x2 And x3 <> "00" Then Rows(i + 1 & ":" & i + 1).Select Selection.Insert Shift:=xlDownd Cells(i + 1, 3) = Cells(i, 3) Cells(i + 1, 4) = Split(Cells(i + 2, 4), ":")(0) & ":00" Cells(i + 1, 5) = Cells(i, 5) nLR = nLR + 1: i = i + 1 End If i = i + 1 LoopEnd Sub
[解决办法]
下列代码在 Excel 2003 下测试通过。
楼主参考一下:
Option ExplicitSub Main() Sheets("sheet1").Select '选定要处理的工作表 Call InstData(2) '插入数据。数据从第2行开始 End SubSub InstData(ByVal iLineBgn&) Dim iLineEnd&, iDate&, iHour&, strTemp$ Dim j&, k& iLineEnd = WorksheetFunction.Count(Range("C:C")) + iLineBgn - 1 strTemp = Cells(iLineBgn, 4) iDate = Int(CDate(strTemp)) iHour = Hour(strTemp) Do While (iLineBgn < iLineEnd) iLineBgn = iLineBgn + 1 strTemp = Cells(iLineBgn, 4).Text k = Hour(strTemp) If (Minute(strTemp)) Then If (k <> iHour) Then Rows(iLineBgn).Insert Shift:=xlDown j = iLineBgn - 1 Cells(iLineBgn, 3).Formula = Cells(j, 3).Formula Cells(iLineBgn, 5).Formula = Cells(j, 5).Formula If (k > 0) Then Cells(iLineBgn, 4).Formula = Left$(strTemp, InStr(1, strTemp, " ")) & k & ":00" Else Cells(iLineBgn, 4).Formula = Left$(strTemp, InStr(1, strTemp, " ")) & "0:00" End If iLineEnd = iLineEnd + 1: iLineBgn = iLineBgn + 1 End If End If iHour = k LoopEnd Sub