更改同一行中相关单元格的更新日期

时间:2014-05-07 03:36:47

标签: excel-vba excel-formula excel-2010 vba excel

参考此链接: Automatic date update in a cell when another cell's value changes (as calculated by a formula)

Roman 的回答是有帮助的。而是存储oldvalue& oldDate用于记录属性,我更喜欢将它们存储在同一行的单元格(表格)中。

这是我尝试更改功能的内容:

Public Function UDF_EditDate(ByVal newData As Range, ByRef oldData As Range, ByRef   oldDate As Range) As Date
If newData.Count = 1 And oldData.Count = 1 And oldDate.Count = 1 Then

    If (oldDate.Value = "") Or (newData.Value <> oldData.Value) Then
        oldData.Value = newData.Value
        Range(oldDate).Value = Now()
    End If

    UDF_EditDate = Now()
End If
End Function

并在公式单元格中说&#34; D1&#34;我说:

= UDF_EditDate(A1,B1,C1)

但是,不幸的是,这个功能并没有按预期工作。

任何人都可以帮我复习并解决我的问题吗?

2 个答案:

答案 0 :(得分:0)

下面的代码在工作表上查看A列,当它更改时,将当前日期放在同一行B列中:

Private Sub Worksheet_Change(ByVal Target As Range)
    If Intersect(Target, Target.Parent.Range("A:A")) Is Nothing Then Exit Sub
    Target.Next.Value = Date
End Sub

只有在更改单个单元格时才能正常工作,尝试自己修改多​​个单元格

答案 1 :(得分:0)

感谢avb

我做了一些修改,将A列扩展为D列,并将更新列在E列的对应行上。

Private Sub Worksheet_Change(ByVal Target As Range)
    If Intersect(Target, Target.Parent.Range("A:D")) Is Nothing Then Exit Sub
    For Each x In Target
        Cells(x.Row, 5).Value = Now
    Next
End Sub