代码的作用:
在C列中输入名称,日期填充在K列中。
其他:
以下是代码:
'Adds date when borrower name is entered
Private Sub Worksheet_Change(ByVal Target As Range)
Dim A As Range, B As Range, Inte As Range, r As Range
Set A = Range("C:C")
Set Inte = Intersect(A, Target)
If Inte Is Nothing Then Exit Sub
Application.EnableEvents = False
For Each r In Inte
If r.Offset(0, 8).Value = "" Then
r.Offset(0, 8).Value = Date
End If
Next r
Application.EnableEvents = True
End Sub

我的代码来自:Auto-fill the date in a cell, when the user enters information in an adjacent cell
我希望代码能做什么:
如果列J是"已注册"将日期添加到列K(当前正在执行)
如果列J更改为"已锁定",请将日期添加到列L
答案 0 :(得分:1)
将目的地的大小调整为两列,然后添加'已注册"以及日期。
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Columns(3), Target) Is Nothing Then
On Error GoTo bm_SafeExit
Application.EnableEvents = False
Dim C As Range
For Each C In Intersect(Columns(3), Target, Target.Parent.UsedRange)
If IsEmpty(C.Offset(0, 8)) Then
C.Offset(0, 7).Resize(1, 2) = Array("registered", Date)
End If
Next C
End If
bm_SafeExit:
Application.EnableEvents = True
End Sub
我发现如果您要处理多个Target单元格(例如,通过粘贴操作),那么通过Target的Intersect和您预定列的循环也应该减少到范围的Worksheet.UsedRange property。没有它,大量的空白单元将在完整的列粘贴操作中循环。