我的工作表现在有以下代码。当我在列E中输入数据时,返回的值如评论的日期" new"将分别在B,D和F栏中正常工作。
但是,当我删除E列上的数据时,B列,D列和F列的返回值仍然存在。
如果删除我在E栏中输入的数据,如何清除它们?
非常感谢!
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"
End If
答案 0 :(得分:1)
如果您的意思是,如果清除列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