且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

通过公式(Excel)更改单元格时的时间戳

更新时间:2023-11-04 12:49:52

更改 MsgBoxValue Changed to:

  Range(B1)。Value = Format(现在,dd / mm / yyyy hh:mm:ss)
pre>

或任何时间戳格式您需要


I need a way to timestamp an adjacent cell whose value changes via formula. Using this as an example I need the cell adjacent to A1 on sheet 1 to timestamp the date and time when the cell value changed.

The example I have linked to above initiates a message box when the cell value changes via formula (worksheet_change events don't appear to recognise changes to a cell value when it contains a formula whose value is changed because of a cell change elsewhere). I don't want the message box but I do want a timestamp.

For simplicity I will post the instructions at that linked question here, any additional help with this specific question is appreciated.

In Sheet1 Cell A1, put this formula

=Sheet2!A1+1

Now In a module paste this code

Public PrevVal As Variant

Paste this in the Sheet Code area

Private Sub Worksheet_Calculate()
    If Range("A1").Value <> PrevVal Then
        MsgBox "Value Changed"
        PrevVal = Range("A1").Value
    End If
End Sub

And lastly in the ThisWorkbook Code area paste this code

Private Sub Workbook_Open()
    PrevVal = Sheet1.Range("A1").Value
End Sub

Change MsgBox "Value Changed" to:

Range("B1").Value = Format(Now, "dd/mm/yyyy hh:mm:ss")

or whichever timestamp format you require