首页 > 解决方案 > ActiveX 组合框不会自动关闭

问题描述

我的ActiveX Combobox主工作表中有一个控制/更新一系列图表。

Private Sub cmBoxSelect_GotFocus()
    Application.ScreenUpdating = False
    With Me.cmBoxSelect
       .List = Array("Grand Total", "Prod1", "Prod2", "Prod3", "Prod4", "Prod5")
       .ListRows = 6
       .DropDown
    End With
    Application.ScreenUpdating = True
End Sub

Private Sub cmBoxSelect_Change()
     'series of codes which manipulates the charts, based on selection...

End Sub

我注意到,当我单击ComboBox并选择其中一个内容时,它会在所选内容上留下蓝色突出显示。所以为了防止这种情况,我补充说:

Private Sub cmBoxSelect_DropButtonClick()
    Application.ScreenUpdating = False
        ActiveCell.Activate
    Application.ScreenUpdating = True
End Sub

它成功地删除了亮点。

但是,它有一个奇怪的缺点。cmbSelect一旦用户没有选择任何内容,就不会自动关闭(一旦组合框处于活动状态并且用户单击工作表中的任何单元格,它就不会关闭)。DropButtonClick在我添加事件之前它正在工作。

我是否错过了上述任何内容或任何错误步骤?感谢您的投入!

编辑#1

似乎我已经通过反复试验找到了解决方案。我只添加了一个空白标签并选择它以在发生更改时将焦点从 ComboBox 中移除。我也将其更改DropButtonClickLostFocus.

Private Sub cmBoxSelect_GotFocus()
    Application.ScreenUpdating = False
    With Me.cmBoxSelect
       .List = Array("Grand Total", "Prod1", "Prod2", "Prod3", "Prod4", "Prod5")
       .ListRows = 6
       .DropDown
    End With
    Application.ScreenUpdating = True
End Sub

Private Sub cmBoxSelect_LostFocus()
    ActiveCell.Select
End Sub

Private Sub cmBoxSelect_Change()
     'series of codes which manipulates the charts, based on selection...

     Me.Label1.Select

End Sub

标签: vbaexcelactivex

解决方案


让我们试试这个:不是由更改触发的事件,而是由 dropbuttonclick

Private Sub changingComboBox(String s)


     'series of codes which manipulates the charts, based on selection...

End Sub

Private Sub cmBoxSelect_DropButtonClick()
    Dim s As String
    s = cmBoxSelect.SelText

    If (cmBoxSelect.SelText = cmBoxSelect.Value) Then

        cmBoxSelect.Value = ""
        cmBoxSelect.Value = s

    Else

        call changingComboBox(cmBoxSelect.Value)

    End If

End Sub

那个怎么样 ?


推荐阅读