【问题标题】:Static date in selected column as formula change value选定列中的静态日期作为公式更改值
【发布时间】:2013-05-25 13:33:19
【问题描述】:

我想通过excel宏来完成这个:

在我的工作簿Sheet1 中,通过公式将值输入到 Range (A2:H20) 中。

我还有 Range (J2:P20),它将跟踪 Range (A2:H20) 中值的变化,并带有此格式的日期戳dd-mmm-yyyy

现在我想这样做:

  • 如果A2 中的值发生更改,则日期戳应反映在J2
  • 如果A3 中的值发生更改,则日期戳应反映在J3
  • 如果B2 中的值发生更改,则日期戳应反映在K2
  • 如果B3 中的值发生更改,则日期戳应反映在K3

这些范围内的其他单元格也是如此。

日期戳应替换其各自单元格中的任何现有日期(如果存在)。 如果 Range (A2:H20) 中的任何单元格为 =0 或为空,则相应单元格中不应有日期戳。

【问题讨论】:

  • 我认为你的意思是 Q20 而不是 P20

标签: excel vba


【解决方案1】:

这是一个仅针对单元格 A2/J2 的示例。如果您想在整个范围内使用它,则必须扩展代码。下面的代码进入工作表的“Worksheet_Change”过程。部分示例来自Microsoft

Private Sub Worksheet_Change(ByVal Target As Range)
Dim KeyCells As Range
'Code Example from Microsoft.com
'http://support.microsoft.com/kb/213612


' The variable KeyCells contains the cells that will
' cause an alert when they are changed.
Set KeyCells = Range("A2:H20")

If Not Application.Intersect(KeyCells, Range(Target.Address)) _
       Is Nothing Then

    ' Display a message when one of the designated cells has been
    ' changed.
    ' Place your code here.
    'MsgBox "Cell " & Target.Address & " has changed."  <--- Original Microsoft Example

    'Code to Check Value/Status of A2
    If Target.Address = "$A$2" Then
        If Range("A2").Value <> "" Then
        Range("J2").Value = Now()
        Else
        Range("J2").Value = ""
        End If
    End If

End If
End Sub

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-28
    • 2019-12-18
    • 1970-01-01
    • 2017-06-25
    • 2020-03-21
    • 2015-10-05
    相关资源
    最近更新 更多