如何在包含公式的单元格的值第一次更新时设置时间戳?

问题描述 投票:0回答:1

我有一本包含三个工作表的工作簿。

Worksheet1具有工作表2和工作表3的汇总。永远不会手动更新Worksheet1,因为数据/信息是在worksheet2 / 3中更新的,并且它通过单元格中的vlookup公式填充到worksheet1中。

我想为工作表1的D列中的单元格第一次包含1%-99%的百分比提供一个时间戳。

  • 我希望此时间戳在输入第一个数据后永不更改。
  • 我也希望时间戳显示在G列中。

我希望工作表1的D列中的单元格第一次包含100%时都要使用另一个时间戳。

  • 我希望此时间戳在输入第一个数据后永不更改。
  • 我也希望在H列中打印时间戳。

仅在手动更改单元格时才会拾取。由于工作表是根据公式填充的,因此它无法捕获更改。

Private Sub Worksheet_Change(ByVal Target As Range)

Dim myTableRange As Range
Dim myDateTimeRage As Range
Dim myUpdatedRange As Range

Set myTableRange = Range("D1:D314")

If Intersect(Target, myTableRange) Is Nothing Then Exit Sub

Set myDateTimeRage = Range("N" & Target.Row)
Set myUpdatedRange = Range("O" & Target.Row)

If myDateTimeRage.Value = "" Then
    myDateTimeRage.Value = Now
End If

myUpdatedRange.Value = Now

End Sub
excel vba excel-formula timestamp worksheet
1个回答
0
投票

这里是与Worksheet_Calculate一起使用的基本代码>

Private Sub Worksheet_Calculate()
Dim myTableRange As Range

Set myTableRange = Range("D1:D30") 'Change the range as needed

    For Each cel In myTableRange 
    'when a cell is changed in the range due to a formula, the code loops through and checks all the cells for a change. 

        If cel.Value >= 0.01 And cel.Value <= 0.99 And cel.Offset(, 3).Value = "" And cel.Offset(, 4).Value = "" Then 
       'If the timestamp is not for columns G and H, change the offset(,3) to 10, and offset(,4) to 11

            cel.Offset(, 3).Value = Now

        ElseIf cel.Value = 1# And cel.Offset(, 4).Value = "" Then
        'Test for 100%

            cel.Offset(, 4).Value = Now
        End If

    Next cel
End Sub
© www.soinside.com 2019 - 2024. All rights reserved.