本文介绍了当"del"键击中合并单元格值Worksheet_Change时未触发的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

单元格E12与单元格F12合并.

Cell E12 is merged with cell F12.

如果我按下删除"键清除了单元格E12的内容,则单元格c39不会改变.

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

如果我使用Backspace + enter清除了单元格E12的内容,则单元格c39确实会更新.

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

推荐答案

如果在按下 Delete 时选择项中包含E12,则此方法将起作用.这是必需的,因为当您按下 Delete 键时,Target.Address被评估为 Range("E12:F12"),但是当您在中输入一个值时E12 Target.Address就是 Range("E12).

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

如果选择了所有E列,单元格A1和E12等,也会触发此操作.这就是 Intersect 操作所做的,我猜这就是您想要的.

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.

请注意,我还添加了代码,以在代码核心运行之前和之后打开和关闭 EnableEvents .这样可以防止您的代码触发其他 Worksheet_Change 事件.

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.

这篇关于当"del"键击中合并单元格值Worksheet_Change时未触发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-22 10:29