VBA自动添加持续时间以开始日期以生成结束日期

时间:2016-12-28 01:50:05

标签: excel vba excel-vba date

我正在尝试根据表格中的开始日期和持续时间生成结束日期。我的表格如下:

Table column for ID, Task Desc, Start Date, Duration, Planned End Date is A,B,C,D,E respectively

我尝试按如下方式运行VBA代码,但它只会在运行sub时生成结束日期。如何将其设为自动生成?代码如下:

Sub PlannedEndDate()

Dim userInputDate As Date
Dim duration As Integer
Dim endDate As Date

Dim MyRow As Long

'Start at row 6
MyRow = 6
'Loop untill column A is empty (no task)
While Cells(MyRow, "A").Value <> ""
    'Only if not yet filled in.
    If Cells(MyRow, "E").Value = "" Then
        'Fetch info
        userInputDate = Cells(MyRow, "C").Value
        duration = Cells(MyRow, "D").Value
        endDate = DateAdd("d", duration, userInputDate)
        'Put it back on the sheet
        Cells(MyRow, "E").Value = endDate
    End If
    MyRow = MyRow + 1
Wend
End Sub

非常感谢。

2 个答案:

答案 0 :(得分:1)

虽然我建议您只使用E栏中的公式(例如,E6会有=IF(OR(C6="",D6=""),"",C6+D6)),但以下Worksheet_Change事件可能会执行您要求的操作。

Private Sub Worksheet_Change(ByVal Target As Range)
    Dim c As Range
    Dim o As Integer
    Application.EnableEvents = False
    'Don't do anything unless something changed in columns C or D
    If Not Intersect(Target, Columns("C:D")) Is Nothing Then
        'Process all changed cells in columns C and D
        For Each c In Intersect(Target, Columns("C:D"))
            With c
                'Ensure that we are on row 6 or later, and
                'column E is empty, and
                'neither column C or column D is empty
                If .Row > 5 And _
                   IsEmpty(Cells(.Row, "E").Value) And _
                   Not (IsEmpty(Cells(.Row, "C").Value) Or IsEmpty(Cells(.Row, "D").Value)) Then
                    'Ensure that column C contains a date, and
                    'column D contains a numeric value
                    If IsDate(Cells(.Row, "C").Value) And _
                       IsNumeric(Cells(.Row, "D").Value) Then
                        'Calculate planned end date
                        Cells(.Row, "E").Value = CDate(Cells(.Row, "C").Value + Cells(.Row, "D").Value)
                    End If
                End If
            End With
        Next
    End If
    Application.EnableEvents = True
End Sub

请注意,对C或D列中单元格的更改将重新计算E列中的值,除非您删除显示IsEmpty(Cells(.Row, "E").Value) And _的位。 (但是,如果你这样做,你也可以使用我推荐的公式。)

答案 1 :(得分:0)

我认为您的数据如下所示

ID  Task Description    Start Date    Duration  Planned End Date
1   subtask1            5-Dec-16           5    
2   subtask2            22-Dec-16          6    
3   subtask3            1-Dec-16           33   
4   subtask4           21-Dec-16           12   
5   subtask5           4-Jan-16            6

将以下代码放在要执行操作的工作表模块中

Private Sub Worksheet_Change(ByVal Target As Range)

If (Target.Column = 3 Or Target.Column = 4) And Target.Row >= 2 Then


    If Cells(Target.Row, 3) = vbNullString Or Cells(Target.Row, 4) = vbNullString Then

    Else
        Cells(Target.Row, 5).Value = Cells(Target.Row, 3).Value + Cells(Target.Row, 4).Value
    End If

End If


End Sub

每次在列开始日期或持续时间中输入数据时,它都会生成输出。