2014-11-14 13 views
0

我在Excel VBA项目中使用了一个组合框,它使用一系列单元格作为项目列表。我在其中使用了一个过滤器,这样,无论何时输入值,列表都会缩小到包含该字符串的项目,并显示下拉列表。但是,出现问题时,显示下拉菜单时,不能使用导航键在项目内滚动。一旦按下向下键,下拉列表将被再次过滤。如何在不选择列出项目的情况下使用组合框keydown事件

我猜想它的发生是因为下侧键同时关注物品,也在选择它。因此,combobox_change事件被自动调用。

有没有办法让我可以停止keydown事件自动选择一个项目,但只能通过它们滚动?

+0

如果列表进行排序,组合框会自动提示下一个有效选项..你不会需要一个过滤器。但无论如何,如果你想拥有这个过滤器函数和keydown事件,你可以在你的keyDown事件中排除导航键。无论是与否或选择的情况下...如果不是KeyCode = 48然后.. – 2014-11-14 15:07:20

+1

感谢您的建议,但我已经尝试过,但问题没有解决。 Vba在使用向下键进行聚焦时自动选择该项目。有什么办法可以阻止它吗? – Prantosh 2014-11-15 09:11:32

回答

1

编辑答案:

现在已经建立了自己的表,并与这些思想工作,具有讽刺意味的Application.EnableEnable只有在特定情况下帮助,因为Combobox_Change()事件仍然禁用(事件触发或者似乎是的情况下,在最小)。我发现的基本想法涉及操纵KeyCodes并设置标志。我下面的例子包括使用ComboBox称为TempCombo和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 

我用Abort变量作为一个标志​​以防止事件代码的多次触发,并允许键不更改链接单元格的文本结果,从而防止更新我的动态范围。确保两个子例程位于ComboBox所在的工作表上!

所以这是我为此做的一个骨架,如果有人发现问题请告诉我,但我希望这可以帮助某人。


老回答

我不知道有多少,这将真正帮助,如果我有 声誉,我只想提出这个作为一个评论,因为这确实不是 有资格作为答案。然而,我有同样的问题,并试图回答这个问题,偶然发现了 。我希望,在代码中的一个 微软的帮助页面: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

似乎看KeyDown事件的向上和向下箭头 和配对与ComboBox_Change事件将让您隔离 他们,然后当您上下按下 来导航列表时,防止更新组合框。

我建议通过OP的后续文章来看,这是令人难以置信的 信息,并帮助我适应我自己的情况。 还注意UserForm和一般Excel VBAcode之间的差异,对于 实例Me.EnableEvents对于UserForm将需要为 Application.EnableEvents一般excel!

我现在知道这是旧的,但在此情况下,任何人帮助,祝你好运!

相关问题