在单元格更新/更改时自动输入日期和时间

时间:2019-05-14 19:46:27

标签: excel vba

背景:

如果“感兴趣的单元格”通过公式更改值,我希望宏在右侧的空白单元格上自动记录时间和日期

例如如果单元格(“ k3”)更改了值,则在单元格(“ L3”)上更改时,则将日期和时间寄存器; 如果cell(“ L3”)不为空,则在cell(“ M3”)中注册TIME&DATE,依此类推,直到找到一个空单元。

到目前为止,每当“目标单元”更改值时,我都无法提示宏。 PS:后者是一个 IF公式,它输出2个可能的字符串:“确定”和“问题风险警告”

我尝试了以下代码:

Private sub Register_timestamp(ByVal Target As Range)
'This sub registers the date and hour at which the cells in column K:K changed values.

    Dim WorkRng As Range
    Dim Rng As Range
    Dim xOffsetColumn As Integer

    Set WorkRng = Intersect(Application.ActiveSheet.Range("K:K"))

    xOffsetColumn = 1

    If WorkRng Is Nothing Then

        Application.EnableEvents = False

        For Each Rng In WorkRng

        If Not VBA.IsEmpty(Rng.Value) Then
            Rng.Offset(0, xOffsetColumn).Value = Now
            Rng.Offset(0, xOffsetColumn).NumberFormat = "dd-mm-yyyy, hh:mm:ss"
        Else
            xOffsetColumn = xOffsetColumn + 1
        End If

        Next

        Application.EnableEvents = True

      End If

End sub

预期输出:

如果我要手动更改受“关注单元”的IF功能约束的单元并触发它,则“关注单元”更改的日期和时间,例如: 14/05/2019 21:44:21

1 个答案:

答案 0 :(得分:0)

这是您实施我的建议的方式。确保此代码位于正确的工作表的代码模块上。

Private Sub Worksheet_Calculate()

    Dim rMonitored As Range
    Dim MonitoredCell As Range
    Dim vSelected As Variant
    Dim aNewValues As Variant
    Dim ixFormulaCell As Long

    On Error Resume Next
    Set rMonitored = Me.Columns("K").SpecialCells(xlCellTypeFormulas)
    On Error GoTo 0
    If rMonitored Is Nothing Then Exit Sub  'No formula cells in column K

    Application.EnableEvents = False    'Disable events to prevent infinite calc loop
    Set vSelected = Selection           'Remember current selection (it may not be a range)

    'Prepare the array that will store the new values, the cells those values are in, and whether or not there was a change
    ReDim aNewValues(1 To rMonitored.Cells.Count, 1 To 3)
        'Column1 = new value
        'Column2 = cell address
        'Column3 = did value change?

    'Get the new value for each formula in column K
    ixFormulaCell = 0
    For Each MonitoredCell In rMonitored.Cells  'The formula cells may not be in a contiguous range
        ixFormulaCell = ixFormulaCell + 1
        aNewValues(ixFormulaCell, 1) = MonitoredCell.Value  'Store the new value
        Set aNewValues(ixFormulaCell, 2) = MonitoredCell    'Store the cell address
    Next MonitoredCell

    Application.Undo    'This will undo the most recent change, which allows us to compare the new vs old to check for formula updates

    ixFormulaCell = 0
    For Each MonitoredCell In rMonitored.Cells
        ixFormulaCell = ixFormulaCell + 1
        'Check if the formula result is different
        If MonitoredCell.Value <> aNewValues(ixFormulaCell, 1) Then
            'Formula result found to be different, record that
            'We can't put the timestamp in now because we still have to redo the most recent change
            aNewValues(ixFormulaCell, 3) = True
        End If
    Next MonitoredCell

    Application.Undo    'Redo the most recent change to put worksheet back in the new state

    'Now that we've completed our comparison and have re-done the most recent change, check what did change and put in a timestamp in the next empty cell in same row
    For ixFormulaCell = LBound(aNewValues, 1) To UBound(aNewValues, 1)
        'Check for formula result change
        If aNewValues(ixFormulaCell, 3) Then
            'Formula result change found, get next empty cell in same row
            With Me.Cells(aNewValues(ixFormulaCell, 2).Row, Me.Columns.Count).End(xlToLeft).Offset(, 1)
                'Next empty cell found, put in the current datetime stamp and format it
                .Value = Now
                .NumberFormat = "dd-mm-yyyy, hh:mm:ss"
            End With
        End If
    Next ixFormulaCell

    vSelected.Select                'Re-select the remembered selection so that this operation is invisible to users
    Application.EnableEvents = True 'Re-enable events so that the next calculation can be monitored for formula changes in cells of interest

End Sub