【问题标题】:Time stamp cell based on value of a different cell changing from a formula基于从公式更改的不同单元格的值的时间戳单元格
【发布时间】:2018-04-03 10:32:14
【问题描述】:

我对 vba 有点陌生,我看到的所有解决方案都集中在更改值而不是公式上。

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

该公式基于其他列的连接。示例:单元格 A2 将是狗和 B2 猫。 R2 将显示 Dog Cat。如果我更改 B2 或 A2,我希望日期戳在 H2 中。

请帮帮我:)

【问题讨论】:

  • 只是提示您为什么会收到反对票:请尝试提供您已经尝试过的最小示例(请参阅:stackoverflow.com/help/mcve)。 Stackoverflow 不是免费工作的地方,我们会帮助您解决代码中的问题

标签: vba excel excel-formula timestamp


【解决方案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

【讨论】:

  • 不确定你在问什么——你能详细说明一下吗?
  • 当工作表中行中的任何单元格发生更改时,这段代码会插入一个时间戳。我在工作表中有一个名为 table6 的表格,其列范围为 A-P。是否只能通过修改上面的代码才能将此代码应用于表格范围?
【解决方案2】:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-09
    • 1970-01-01
    • 2015-07-28
    • 2018-08-24
    • 2020-07-31
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多