我有一个下拉数据验证其将选择的选项的值保存到另一单元格位置。这是用做:
Private Sub Worksheet_Change(ByVal Target as range)
当我试着输入字符串到它,错误信息会弹出这是意料之中的,因为我们不能输入任何东西到下拉菜单,除非它是在列表中。然而,每当我尝试点击“重试或取消”的错误消息框,更改事件将再次触发。
我已经有了
Application.EnableEvents = False
但仍然没有工作。你能帮我么?我不想,如果我遇到一个错误消息,由于下拉再次触发Change事件。
下面是代码和一个示例场景:
Private Sub Worksheet_Change(ByVal Target As Range)
Application.EnableEvents = False
Cells(1, 2) = Target.Value
Application.EnableEvents = True
Debug.Print Cells(1, 2).Value
End Sub
细胞(1,1)=含有下拉列表(A,B,C)
方案1:我将从列表中选择一个,将遇到的任何错误。
Debug.print:
一种
方案2:我将输入的字符“W”到下拉列表中,会遇到错误。
错误:
值不匹配在该单元定义的数据有效性限制。
Debug.Print:
A A A
正如你所看到的,更改事件情景1后,再次触发了两次。
提前致谢!
Ĵ
您允许更改事件,如果工作表中的任意单元格的变化被解雇。你不需要启用事件的限制,只要指定目标......
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address = "$A$1" Then
If Target.Value = "A" Or Target.Value = "B" Or Target.Value = "C" Then
Cells(1, 2) = Target.Value
End If
End if
End Sub