Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
VBA自动将持续时间添加到开始日期以生成结束日期_Vba_Excel_Date - Fatal编程技术网

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

VBA自动将持续时间添加到开始日期以生成结束日期,vba,excel,date,Vba,Excel,Date,我正在尝试根据表中的开始日期和持续时间生成结束日期。我的表格如下: 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 <>

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

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

我已尝试按如下方式运行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
子计划结束日期()
Dim userInputDate作为日期
将持续时间设置为整数
Dim endDate作为日期
让我的行变长
'从第6排开始
MyRow=6
'循环直到列A为空(无任务)
While单元格(MyRow,“A”)。值“”
“除非还没有填写。
如果单元格(MyRow,“E”).Value=“”,则
'获取信息
userInputDate=单元格(MyRow,“C”)。值
持续时间=单元格(MyRow,“D”)。值
endDate=DateAdd(“d”,持续时间,userInputDate)
“把它放回床单上
单元格(MyRow,“E”)。值=结束日期
如果结束
MyRow=MyRow+1
温德
端接头

非常感谢。

< P>我认为你的数据如下:

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

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

虽然我建议您只使用E列中的公式(例如E6将具有
=IF(或(C6=”“,D6=“”),“”,C6+D6)
),但以下
工作表更改事件可能会满足您的要求

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(单元格(.Row,“E”).value)和
的位。(但是,如果您这样做,您也可以使用我推荐的公式。)

您希望何时生成它?如果它是在工作表创建后立即生成的,那么最终会有很多单元格显示“1900年1月1日”。(您是否可以将公式
=C6+D6
放入单元格E6等,并允许用户根据需要重新键入值,或者您的意图是用户将更新“开始日期”或“持续时间”,而您不希望“计划结束日期”从最初填充这些单元格时更改?)我希望每次填写开始日期和持续时间单元格时生成结束日期,如果可能,我不希望用户重新键入为计划结束日期填充的值。因此,为了确定您的问题,如果用户在单元格C10中输入2017年1月1日的日期,然后在单元格D10中输入1000的持续时间,您希望将单元格E10设置为2019年9月28日。然后,当用户意识到他们只想在单元格D10中放入100(而不是1000)时,您希望E10保持在2019年9月28日?我希望它是自动化的。用户输入开始日期和持续时间后,将自动生成结束日期。我读到关于工作表更改事件的信息。但当我尝试使用私有子工作表_Change(ByVal Target作为范围)时,出现了错误。如果用户更改持续时间值,则结束日期将更改。这就是我试图实现的目标如果您希望不时更新结束日期以反映开始日期/持续时间,我认为您只需要在单元格E6中使用
=IF(或(或(C6=“”,D6=“”),“”,C6+D6)
,并将其复制到列E中的所有其他单元格。您的代码当前状态(以及您要求的状态)将值设置为常量,然后在设置后再也不会更改它(因为您特别说
If Cells(MyRow,“E”).value=”“then
)-这意味着错误将永远保留为错误。非常感谢!!它工作得很好。通过查看您的代码,我现在更好地理解了如何将子代码与工作表更改事件集成。