0

我想通过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空或为空,则相应单元格中不应有日期戳。

4

1 回答 1

0

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

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
于 2013-05-30T05:32:39.473 回答