我是这个论坛的第一次用户。这是我的场景:在用户表单上,我有一个组合框、两个文本框和一个“确定”按钮。当用户从组合框的下拉列表中进行选择时,组合框的更改事件将被触发,事件处理代码将根据用户的选择使用工作表中的信息填充文本框。然后,用户可以编辑一个或两个文本框中的信息。然后用户点击“确定”。然后,“确定”按钮的单击事件将文本框中修改的信息写回工作表中的单元格。看起来相当简单。这是我的问题:组合框的更改事件似乎在每次引用其属性时都会触发。具体来说,下面的 cb_CustomersUpdateOK_Click() 子例程中的三个实例引用了组合框的 ListIndex 属性。我在 Change 事件代码中放置了一个 Msgbox 以指示事件何时触发。在 OK 单击事件代码中的三个分配语句中的每一个处使用断点,在这三个语句中的每一个处触发组合框(显示消息框)。当触发发生时,它会使用组合框选择中的初始数据覆盖文本框中编辑的信息。 (1) 为什么组合框更改事件会这样触发? (2) 我应该怎样做才能避免这种情况发生?
过去几个小时我一直在研究这个问题,但没有发现任何非常有用的东西。任何帮助将不胜感激。如果需要更多信息,请告诉我。
Private Sub combo_CustomersUpdateLastName_Change()
MsgBox "combobox changed" 'For debug purposes
With Sheets("Customers")
tb_CustomersUpdateFirstName.Value = .Cells(combo_CustomersUpdateLastName.ListIndex + 2, 2).Value
tb_CustomersUpdatePhone.Value = .Cells(combo_CustomersUpdateLastName.ListIndex + 2, 3).Value
End With
End Sub
...
Private Sub cb_CustomersUpdateOK_Click()
'Copy the updated customer data from the controls to the Customers sheet
With Sheets("Customers")
.Cells(combo_CustomersUpdateLastName.ListIndex + 2, 1).Value = combo_CustomersUpdateLastName.Value
.Cells(combo_CustomersUpdateLastName.ListIndex + 2, 2).Value = tb_CustomersUpdateFirstName.Value
.Cells(combo_CustomersUpdateLastName.ListIndex + 2, 3).Value = tb_CustomersUpdatePhone.Value
'Sort the customer data in case the last name or first name was updated
Range("CustomerInfo").Sort Key1:=.Columns("A"), Key2:=.Columns("B")
End With
MsgBox "Customer data updated."
Unload form_CustomersUpdate
End Sub
组合框项目的来源是什么?如果它链接到一个范围,则会触发更改事件,因为写入工作表会更改链接的源,这又意味着组合框会自行刷新。
如果您不希望发生这种情况,那么您需要将范围中的值“缓存”到数组中,并用它们填充组合框。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)