When 'del' key hit on merged cell value Wo

2019-08-06 05:14发布

Cell E12 is merged with cell F12.

If I clear the contents of cell E12 by hitting the "delete" key, cell c39 doesn't change.

If I clear the contents of the cell E12 by using backspace+enter, cell c39 does update.

Private Sub Worksheet_Change(ByVal Target As Range)
    If Target.Address = "$E$12" Then
        Range("d28").Value = Range("e12").Value

        If Range("e12") = "" Then ' update cell c39 with calculator
            Range("c39") = "Do you ?"
        Else
            Range("c39") = "Do you " & Range("e13").Text & "?"
        End If
    End If

End Sub

标签: excel vba
1条回答
手持菜刀,她持情操
2楼-- · 2019-08-06 05:45

This will work if E12 is included in the selection when Delete is pressed. This is necessary because when you hit the Delete key, Target.Address is evaluated as Range("E12:F12"), but when you enter a value in E12 Target.Address is just Range("E12).

Private Sub Worksheet_Change(ByVal Target As Range)
On Error GoTo Reset_EnableEvents
Application.EnableEvents = False
If Not Intersect(Target, Range("$E$12")) Is Nothing Then
    Range("d28").Value = Range("e12").Value
    If Range("e12") = "" Then    ' update cell c39 with calculator
        Range("c39") = "Do you ?"
    Else
        Range("c39") = "Do you " & Range("e13").Text & "?"
    End If
End If
Reset_EnableEvents:
Application.EnableEvents = True
End Sub

This will also be triggered if all of column E is selected, cells A1 and E12, etc. That's what the Intersect operation does, and I'm guessing that's what you want.

Note that I also added code to turn EnableEvents off and on, before and after the heart of your code runs. This keeps your code from triggering additional Worksheet_Change events.

查看更多
登录 后发表回答