EXCEL查找与自动输入日期代码

Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Count > 1 Then Exit Sub
If Target.Column = 1 And Target.Value <> "" Then
Target.Offset(0, 1) = Format(Now, "yyyy-mm-dd hh:mm:ss")
End If
If Target.Column = 3 And Target.Value <> "" Then
If [A:A].Find(Target, lookat:=xlWhole) Is Nothing Then Exit Sub
If Cells([A:A].Find(Target, lookat:=xlWhole).Row, 4) = "" then Cells([A:A].Find(Target, lookat:=xlWhole).Row, 4) = Format(Now, "yyyy-mm-dd hh:mm:ss")
End If
End Sub

代码可以实现在A列输入内容,在B列的同一行自动输入日期;另外在C列输入内容,可以在A列查找与之相同的数据的行,然后在D列的同一行自动输入日期。
但这个代码如何加以修改,能够实现:
当D列的那行不为空时,才自动输入日期,且要找到A列所有与C列输入内容相同的行,并在D列自动输入日期。上面的代码不能实现,当C列8行输入2时,在D列7行自动输入日期——如何修改代码实现这一点?

Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Count > 1 Then Exit Sub
If Target.Column = 1 And Target.Value <> "" Then
Target.Offset(0, 1) = Format(Now, "yyyy-mm-dd hh:mm:ss")
End If
If Target.Column = 3 And Target.Value <> "" Then
Set c = [A:A].Find(Target, lookat:=xlWhole)
If c Is Nothing Then Exit Sub
x = c.Address
Do
If Cells(c.Row, 4) = "" Then
Cells(c.Row, 4) = Format(Now, "yyyy-mm-dd hh:mm:ss")
Exit Do
End If
Set c = [A:A].Find(Target, after:=c, lookat:=xlWhole)
Loop Until x = c.Address
End If
End Sub
温馨提示:答案为网友推荐,仅供参考
第1个回答  2015-07-06
你第二行还有个2,D2单元格不为空,所以不触发任何操作
相似回答