如果删除其他单元格所依赖的主数据,如何清除其他单元格的内容?

我现在有我的工作表的以下代码。 当我在列E中input数据时,评论“新”的返回值如列B,D和F分别工作正常。

但是,当我删除列E上的数据时,在列B,D和F的返回值保持在那里。

如果我删除了E栏中input的数据,如何清除它们?

非常感谢!

私人小组Worksheet_Change(BYVAL目标作为范围)

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 

如果你的意思是说,如果你清除了列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