我的 Excel VBA 项目中有一个组合框,它使用一系列单元格作为项目列表。我在其中使用了一个过滤器,这样每当输入一个值时,列表就会缩小到包含该字符串的项目,并显示下拉列表。但是,问题出现了,当显示下拉菜单时,导航键无法用于在项目内滚动。一旦按下向下键,下拉列表将再次过滤。
我猜它的发生是因为向下键在关注项目时也选择了它。因此,combobox_change 事件会被自动调用。
有没有办法让我可以停止 keydown 事件自动选择一个项目,而只滚动它们?
最佳答案
编辑答案:
现在已经构建了我自己的工作表并使用了这些想法,具有讽刺意味的是 Application.EnableEvents
仅在某些情况下有帮助,因为 Combobox_Change()
事件仍然会在禁用事件的情况下触发(或者至少看起来是这样)。我发现的基本思想涉及操作 KeyCodes
和设置标志。我下面的示例涉及使用名为 TempCombo
的 ComboBox
并在工作表的 VBA 中的 TempCombo_KeyDown()
事件之后运行代码(我已修剪了我的出于示例目的,将其记录下来):
Option Explicit
Dim Abort as Boolean
Private Sub TempCombo_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
Select Case KeyCode
Case 38 'Up
If TempCombo.ListIndex <= 0 Then KeyCode = 0 'ignore "Up" press if already on the first selection, or if not on a selection
Abort = True
If Not KeyCode = 0 Then ' If on a selection past the first entry
KeyCode = 0
'Manually choose next entry, cancel key press
TempCombo.ListIndex = TempCombo.ListIndex - 1
End If
Me.TempCombo.DropDown
Case 40 'Down
If TempCombo.ListIndex = TempCombo.ListCount - 1 Then Keycode = 0
' This method was from the discussion I linked, prevents "falling off the bottom of the list"
Abort = True
If Not KeyCode = 0 Then ' If on a selection before the last entry
KeyCode = 0
'Manually choose next entry, cancel key press
TempCombo.ListIndex = TempCombo.ListIndex + 1
End If
Me.TempCombo.DropDown
End Select
Abort = False
End Sub
Private Sub TempCombo_Change()
If Abort Then Exit Sub ' Stop Event code if flag set
Abort = True
' sets the flag until finished with commands to prevent changes made by code triggering the event multiple times
' ~~~ Insert Code you want to run for other cases here ~~~
Abort = False
End Sub
我在 TempCombo_Change()
中使用了 Abort
变量作为标志,以防止多次触发事件代码,并允许按键不更改链接的单元格,阻止我的动态范围更新。确保两个子例程都位于 ComboBox
所在的工作表上!
这就是我为此所做的工作的框架,如果有人发现问题请告诉我,但我希望这可以帮助别人。
Old Answer
I'm not sure how much this will truly help, and if I had the reputation I would just submit this as a comment because this really doesn't qualify as an answer. However I had the same question and stumbled across a thread trying to answer this. I hope that one of the codes on one of Microsoft's help pages: http://answers.microsoft.com/en-us/office/forum/office_2007-customize/disable-userform-combobox-change-event-when-arrow/598b44a1-dcda-4a2c-8e12-2b84762f98ae?db=5
It seems that looking at the
KeyDown
events for the up and down arrows and pairing with theComboBox_Change
events would allow you to isolate them and then prevent the combobox from updating when you press up and down to navigate the list.I recommend looking through the OP's follow up post, it is incredibly informative and helped me adapt to my own case. Also note differences between
UserForm
and general Excel VBAcode, for instanceMe.EnableEvents
forUserForm
would need to beApplication.EnableEvents
for general excel!I know this is old now, but in case this helps anyone, good luck!
关于vba - 如何在不选择列出的项目的情况下使用组合框按键事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26931436/