Excel,单元格中的语句没有像预期的那样消失

Excel, statement in cell doesn't disappear like expected

当我在 Sheet 2 D1 中输入一个值时,应该在 table 之类的 if 语句中输入该数字,然后该语句应该消失,并且仍应输入该值在单元格中。

Table:

代码:

Private Sub Worksheet_Change(ByVal Target As Range)
    Dim OperationalArea As Range, AffectedArea As Range
    Set OperationalArea = Me.Range("B2:G7")
    Set AffectedArea = Intersect(Target, OperationalArea)
    
    If Not AffectedArea Is Nothing Then
        Application.EnableEvents = False
        AffectedArea.Value = AffectedArea.Value
        Application.EnableEvents = True
    End If
End Sub

如果更改仅由重新计算引起,事件 WorkSheet_Change() 将不会触发。

您可以改用 Sheet2 中的事件:

' Event in Sheet2
Private Sub Worksheet_Change(ByVal Target As Range)
    
    If Not Application.Intersect(Target, Me.Range("D1")) Is Nothing Then
    
        ' Change "Sheet1" to the name of the sheet with the table in B2:G7
         ThisWorkbook.Worksheets("Sheet1").Range("B2:G7") = Me.Range("D1")
    
    End If
    
End Sub