2016-02-26 45 views
0

我不确定如何解释这个问题,但我会尽我所能解释我需要完成的逻辑。希望在这个网站的任何辉煌的人可以投入一些想法:)使excel vba根据自定义条件在行之间插入自定义数据行数

我有数据记录,其中包含有关不同项目的信息。每行都包含项目信息,例如项目名称,创建日期项目,完成日期项目,项目完成的估计日期以及插入/更新估计的时间戳。如果项目具有更新的项目完成预计日期,则将此更新记录在新行中。这就是数据在excel中的样子。

enter image description here

我需要擅长以检查是否有一天,在预计完成日期(即该项目停留在轨道),那么出类拔萃,直到达到一天一个包含更新创建行没有变化通过。下面的图片显示了我需要如何根据上面的初始行添加自定义行。

enter image description here

请让帮我出任何想法.. VBA建议。

+0

考虑显示到目前为止你已经尝试过的东西,它会很长的路要走使它看起来就像你不能简单地要求其他人做你的工作,你...如果你*从字面上*不知道从哪里开始,请阅读“Do/While”和“ For/Next'循环。 –

+0

@DavidZemens谢谢!我试图用excel公式来实现,但似乎没有一个简单的解决方案...我会给vba一个镜头...另外,我不要求任何人为我做我的工作,但我是把我的问题扔在那里,以防万一有人遇到类似的谜题,我会做剩下的事情,如果别人做了这些事情,我不必重新发明轮子...... – exlover

+0

绝对无法完成公式,因为它们无法操纵表单/行。 –

回答

0

我相信这应该实现自己的目标:

Sub FillCompletionDays() 

Dim LLoop As Long 
Dim LLRow As Long 
Dim DateEnd As Date 
Dim DateNext As Date 
Dim DateNow As Date 

LLoop = Range("A:A").Find(what:="Project name").Row + 1 
LLRow = Range("A:A").Find(what:="*", searchdirection:=xlPrevious).Row 

If LLRow <= LLoop Then Exit Sub 

Do 
    'Only proceed if there is a valid date in column E 
    If Range("E" & LLoop).Value2 <> vbNullString Then 
     DateNow = Range("E" & LLoop).Value2 
     DateEnd = Range("C" & LLoop).Value2 
     'Check if another date is needed 
     If DateEnd > DateNow Then 
      'Check if next row is this project 
      If Range("A" & LLoop + 1).Value2 = Range("A" & LLoop).Value2 Then 
       'Check if a new date is needed 
       DateNext = DateSerial(Year(Range("E" & LLoop + 1).Value2), Month(Range("E" & LLoop + 1).Value2), _ 
       Day(Range("E" & LLoop + 1).Value2)) 
       If DateNext <> DateNow + 1 Then 
        'Insert a row 
        Rows(LLoop + 1).Insert shift:=xlShiftDown 
        Range("A" & LLoop + 1 & ":D" & LLoop + 1).Value2 = Range("A" & LLoop & ":D" & LLoop).Value2 
        Range("E" & LLoop + 1).Value2 = DateNow + 1 
        Range("B" & LLoop + 1 & ": E" & LLoop + 1).NumberFormat = "yyyy-mm-dd" 
        LLRow = LLRow + 1 
       End If 

      Else 
       'Next row is another project; insert a row for this one 
       Rows(LLoop + 1).Insert shift:=xlShiftDown 
       Range("A" & LLoop + 1 & ":D" & LLoop + 1).Value2 = Range("A" & LLoop & ":D" & LLoop).Value2 
       Range("E" & LLoop + 1).Value2 = DateNow + 1 
       Range("B" & LLoop + 1 & ": E" & LLoop + 1).NumberFormat = "yyyy-mm-dd" 
       LLRow = LLRow + 1 
      End If 
     End If 
    End If 
    LLoop = LLoop + 1 
Loop Until LLoop > LLRow 

End Sub 
+0

谢谢尼克..你的代码很有帮助!我已更新您的代码以适合我的需求,以及我希望它的功能如何! – exlover

0

这里是答案编辑@Nick Peranzi回答适合我的请求后,我的问题,我不知道如何标记/提他,但是这是他的用户链接 https://stackoverflow.com/users/5472502/nick-peranzi

Sub FillCompletionDays() 

Dim LLoop As Long 
Dim LLRow As Long 
Dim DateEnd As Date 
Dim DateNext As Date 
Dim DateNow As Date 

LLoop = Range("A:A").Find(what:="Project name").Row + 1 
LLRow = Range("A:A").Find(what:="*", searchdirection:=xlPrevious).Row 

If LLRow <= LLoop Then Exit Sub 

Do 
'Only proceed if there is a valid date in column E 
If Range("E" & LLoop).Value2 <> vbNullString Then 
    DateNow = DateSerial(Year(Range("E" & LLoop).Value2), Month(Range("E" & LLoop).Value2), _ 
      Day(Range("E" & LLoop).Value2)) 
    DateEnd = DateSerial(Year(Range("D" & LLoop).Value2), Month(Range("D" & LLoop).Value2), _ 
      Day(Range("D" & LLoop).Value2)) 
    'Check if another date is needed 
    If DateEnd > DateNow Then 
     'Check if next row is this project 
     If Range("A" & LLoop + 1).Value2 = Range("A" & LLoop).Value2 Then 
      'Check if a new date is needed 
      DateNext = DateSerial(Year(Range("E" & LLoop + 1).Value2), Month(Range("E" & LLoop + 1).Value2), _ 
      Day(Range("E" & LLoop + 1).Value2)) 
      If DateNext = DateNow Then 
      Else 
      If DateNext <> DateNow + 1 Then 
       'Insert a row 
       Rows(LLoop + 1).Insert shift:=xlShiftDown 
       Range("A" & LLoop + 1 & ":D" & LLoop + 1).Value2 = Range("A" & LLoop & ":D" & LLoop).Value2 
       Range("E" & LLoop + 1).Value2 = DateNow + 1 
       Range("B" & LLoop + 1 & ": E" & LLoop + 1).NumberFormat = "yyyy-mm-dd" 
       LLRow = LLRow + 1 
      End If 
      End If 

     Else 
      'Next row is another project; insert a row for this one 
      Rows(LLoop + 1).Insert shift:=xlShiftDown 
      Range("A" & LLoop + 1 & ":D" & LLoop + 1).Value2 = Range("A" & LLoop & ":D" & LLoop).Value2 
      Range("E" & LLoop + 1).Value2 = DateNow + 1 
      Range("B" & LLoop + 1 & ": E" & LLoop + 1).NumberFormat = "yyyy-mm-dd" 
      LLRow = LLRow + 1 
     End If 
    End If 
End If 
LLoop = LLoop + 1 
Loop Until LLoop > LLRow 


End Sub