【发布时间】:2016-10-04 11:07:05
【问题描述】:
我的电子表格上有一个 activeX 下拉表单,它在 _Change 上执行代码。我的代码修改了下拉列表源(添加或删除项目)。每当发生这种情况时,都会再次调用 _Change。
我有各种解决方法,所有这些都是更改列表源的某个版本,但没有成功。这一切都不起作用的原因是清除或更改.ListFillRange 实际上会再次触发_Change 事件。
如果我想在.ListFillRange 中添加或删除项目,如何防止_Changeevent 被调用
更新 w EnableEvents 设置为 false:
Public Sub SetRangeForDropdown()
On Error Resume Next
Application.EnableEvents = False
'Get new List of employees from Employee sheet
Dim rng1 As Range
With wsDB_employee
Set rng1 = .Range("A2:B" & .Range("A10000").End(xlUp).Row)
End With
With wsStage
.Cells.Clear
rng1.Copy .Range(.Cells(1, 1), .Cells(rng1.Rows.Count, 2))
End With
'Set range for dropdown on employee sheet
Dim rng2 As Range
Set rng2 = wsStage.Range("A1:B" & wsStage.Range("A10000").End(xlUp).Row)
'Update employee list named formula
ActiveWorkbook.Names.Add Name:="nfEmployeeList", RefersTo:=rng2
Dim str As String
str = rng2.Parent.Name & "!" & rng2.Address 'Source path for list fill range
wsMA.cmbEmployeeSelection.ListFillRange = str
Application.EnableEvents = True
End Sub
显然EnableEvents 不适用于 ActiveX 控件。
感谢微软让生活变得更加复杂!
刚刚发现:“Application.EnableEvents=False/True ONLY 适用于工作表和工作簿事件,不适用于 ActiveX 控件事件”来自此处enter link description here
【问题讨论】: