如果你的意思是,如果你清除 E 列的内容,然后清除 B、D 和 F 列中的内容,然后使用下面的代码
(但是,为什么每次单元格更改时都需要扫描整行?)
Private Sub Worksheet_Change(ByVal Target As Range)
Dim i As Integer
For i = 2 To 10000
If Cells(i, "E").Value <> "" And Cells(i, "B").Value = "" Then
Cells(i, "B").Value = Date
Cells(i, "B").NumberFormat = "dd.mm.yyyy"
Cells(i, "D").Value = "NEW"
Cells(i, "F").Value = "NEW"
Else
If Cells(i, "E").Value = "" Then
Cells(i, "B").ClearContents
Cells(i, "D").ClearContents
Cells(i, "F").ClearContents
End If
End If
Next i
End Sub
改进的代码:仅在 E 列中的单元格发生更改时运行代码,在这种情况下,仅修改该行的 B、D 和 F 列中的单元格的值。
Private Sub Worksheet_Change(ByVal Target As Range)
Dim WatchRange As Range
Dim IntersectRange As Range
' can modify it to your need, also using dynamic last row with data
Set WatchRange = Range("E2:E10000")
Set IntersectRange = Intersect(Target, WatchRange)
' check values in Column E, only if cells in Column E are modified
If Not IntersectRange Is Nothing Then
Dim i As Integer
' change value only for relevant row change
i = Target.Row
If Cells(i, "E").Value <> "" And Cells(i, "B").Value = "" Then
Cells(i, "B").Value = Date
Cells(i, "B").NumberFormat = "dd.mm.yyyy"
Cells(i, "D").Value = "NEW"
Cells(i, "F").Value = "NEW"
Else
If Cells(i, "E").Value = "" Then
Cells(i, "B").ClearContents
Cells(i, "D").ClearContents
Cells(i, "F").ClearContents
End If
End If
End If
End Sub