基于不同单元格的值的时间戳单元格从公式

时间:2018-04-03 10:32:15

标签: vba excel-vba excel-formula timestamp excel

我是vba的新手,我所看到的所有解决方案都专注于更改值而不是公式。

我在sheet2上有一个名为“table1”的表。当列R调用更改时,它应该在日期列H中为时间戳标记

该公式基于其他列的串联。示例:单元格A2将是Dog和B2 Cat。 R2会显示Dog Cat。如果我更改B2或A2,我希望日期戳位于H2。

请帮帮我:)

2 个答案:

答案 0 :(得分:1)

我发现这有助于:

Private Sub Worksheet_Change(ByVal Target As Range)
Dim r As Range, c As Range
Const DateStampColumn As Long = 8    'Date stamp column number
For Each r In Target.Rows
    For Each c In r.Cells
        If Not IsEmpty(c) Then
            Application.EnableEvents = False
            Cells(r.Row, DateStampColumn).Value = Date
            Application.EnableEvents = True
            Exit For
        End If
    Next c, r 
End Sub

答案 1 :(得分:0)

您可以限制对表格的更改,就像这样。无需单独循环遍历行和列。

Private Sub Worksheet_Change(ByVal Target As Range)

Dim r As Range, r1 As Range
Const DateStampColumn As Long = 8    'Date stamp column number

Set r1 = Intersect(Target, Sheet1.ListObjects("Table6").DataBodyRange)

If Not r1 Is Nothing Then
    For Each r In r1
        If Not IsEmpty(r) Then
            Application.EnableEvents = False
            Cells(r.Row, DateStampColumn).Value = Date
            Application.EnableEvents = True
            Exit For
        End If
    Next r
End If

End Sub