如果另一个单元格更改值,则在单元格中插入函数

时间:2017-03-16 12:15:09

标签: excel vba excel-vba

Private Sub Assumption1Change(ByVal Target As Range)
    Application.EnableEvents = False
    On Error GoTo ErrHandler

    If Target.Text = "D10" Then
    Range("B10").Formula = "=Now()"
    Else
    End If

    Applicatoin.EnableEvents = True
End Sub

如果另一个单元格的值发生变化,我正在使用上面的代码将函数插入到单元格中,尽管我无法使其工作。有人能指出我正确的方向吗?

1 个答案:

答案 0 :(得分:1)

请尝试以下代码,只需确保将其放在您希望其工作的正确工作表中:

Private Sub Worksheet_Change(ByVal Target As Range)

    Application.EnableEvents = False

    If Not Intersect(Range("D10"), Target) Is Nothing Then
        Target.Offset(, -2).Formula = "=Now()" '<-- place the formula 2 columns to the left of the cell you just modified
    End If

    Application.EnableEvents = True ' <-- restore original setting

End Sub