【问题标题】:VBA ComboBox Change Event not triggered未触发 VBA 组合框更改事件
【发布时间】:2019-05-01 16:54:54
【问题描述】:

ComboBox 事件处理程序有这个问题。

我设法创建(并填充项目)我想要的组合框,代码似乎工作正常。但是在程序运行后,如果我尝试在其中一个组合框中选择一项常规项目,似乎没有调用 _Change 方法 --> 我无法处理更改事件。

这是我的类模块(类名:“DB_ComboBox”)

    Option Explicit

    Public WithEvents DB_ComboBoxEvents As MSForms.ComboBox
    Private DB_ComboBox_Line As Integer

    Private Sub DB_ComboBoxEvents_Change()
        MsgBox ("Line : " & DB_ComboBox_Line)
        'Here I want handle The comboboxes changes
        'But this routine is not called!

    End Sub

    Sub Box(CBox As MSForms.ComboBox)
        Set DB_ComboBoxEvents = CBox
    End Sub


    Public Property Let Line(value As Integer)
        DB_ComboBox_Line = value
    End Property

    Public Property Get Line() As Integer
        Line = DB_ComboBox_Line
    End Property

这是我的“主模块”,我在其中创建组合框并将它们传递给“DB_ComboBox”的集合

        Sub CreateComboBox(IncCBoxes)

        Dim curCombo As MSForms.ComboBox
        Dim rng As Range
        Dim tot_items As Integer
        Dim incAddItem As Integer
        Dim incAddItemBis As Integer
        Dim itemBaseArray() As String
        Dim TEMP_ComboBoxInst As New DB_ComboBox


        Set rng = ActiveSheet.Range("J" & IncCBoxes)

        Set curCombo = ActiveSheet.OLEObjects.Add(ClassType:="Forms.ComboBox.1", Link:=False, DisplayAsIcon:=False, Left:=rng.Left, Top:=rng.Top, Width:=rng.Width, Height:=rng.Height).Object



         'Add the items
        itemBaseArray = Split(Foglio7.Cells(IncCBoxes, DBColFileComboIndexErrori), ";")

        For incAddItem = 0 To UBound(itemBaseArray)

            Dim itemLastArray() As String
            itemLastArray = Split(itemBaseArray(incAddItem), ",")

            For incAddItemBis = 0 To UBound(itemLastArray)
                curCombo.AddItem (itemLastArray(incAddItemBis))
            Next

        Next


        TEMP_ComboBoxInst.Box curCombo
        TEMP_ComboBoxInst.Line = IncCBoxes
        customBoxColl.Add TEMP_ComboBoxInst


    End Sub

谁能告诉我我错过了什么?

非常感谢

【问题讨论】:

  • 您是否仔细检查以确保事件已启用? Application.EnableEvents = True
  • 感谢您的回复。我在 Sub CreateComboBox 的开头放置了“Application.EnableEvents = True”,但似乎没有任何改变。我应该把它放在别的地方吗?
  • @dwirony 我以前错了很多次;-)
  • 名称中的下划线肯定没有帮助,但我已经能够让Test_Button_Click 处理程序运行,所以我删除了我的答案。
  • customBoxColl 在哪里以及如何声明?

标签: excel vba class events combobox


【解决方案1】:

这看起来像是一个时间问题: 在另一个打开的文件中运行此代码将起作用。在同一个文件中它没有。 将添加到您的类与添加 OLEControl 分开,即: 立即使用 Application.ontime

见下面的代码:

Private customBoxColl As New Collection

Sub CreateComboBox(IncCBoxes As Long)

        Dim curCombo As MSForms.ComboBox
        Dim rng As Range
        Dim tot_items As Integer
        Dim incAddItem As Integer
        Dim incAddItemBis As Integer
        Dim itemBaseArray() As String
        Dim itemLastArray() As String

        Set rng = ActiveSheet.Range("J" & IncCBoxes)

        With ActiveSheet.OLEObjects.Add(ClassType:="Forms.ComboBox.1", Link:=False, DisplayAsIcon:=False, Left:=rng.Left, Top:=rng.Top, Width:=rng.Width, Height:=rng.Height)
            Set curCombo = .Object
        End With

         'Add the items
        itemBaseArray = Split(Foglio7.Cells(IncCBoxes, DBColFileComboIndexErrori), ";")
        For incAddItem = 0 To UBound(itemBaseArray)
            itemLastArray = Split(itemBaseArray(incAddItem), ",")
            For incAddItemBis = 0 To UBound(itemLastArray)
               curCombo.AddItem (itemLastArray(incAddItemBis))
            Next
        Next
    Application.OnTime Now, "'CallToClass """ & curCombo.Name & """,""" & IncCBoxes & "'"
End Sub

Sub CalltoClass(ctl As String, myline As Long)
Dim TEMP_ComboBoxInst As New DB_ComboBox
        TEMP_ComboBoxInst.Box ActiveSheet.OLEObjects(ctl).Object
        TEMP_ComboBoxInst.line = myline
        customBoxColl.Add TEMP_ComboBoxInst
End Sub

【讨论】:

  • 您好,很抱歉,我不明白您的确切意思。您建议将“Application.ontime”放在哪里?在子函数内部,在两个添加之间?或者我应该将这两个添加放在两个不同的子函数中,然后将其中一个作为“过程所需的参数”传递? link谢谢
  • 先在单独的过程中添加组合框,然后在其他过程中将组合框添加到类中
  • 我试过了,但是好像还是不行。有什么想法吗?
  • 谢谢@EvR,它现在可以工作了。可能我没有正确使用 Application.OnTime,我将不得不亲自调查。非常感谢您的努力
【解决方案2】:

我知道这不适用于您的具体问题,但我只会在此处发布此问题,以供可能遇到此问题的其他人使用。就我而言,事件停止触发是因为我刚刚将数据库复制到了新的 Github 存储库中。

在重新打开 Access 时,事件并没有触发,而前一天它们还不错,这完全让我感到困惑,特别是因为 SO 答案似乎都没有解决我的问题。基本上,Access 会阻止宏和代码,并要求通过单击屏幕顶部黄色小警告上的“确定”来重新启用它。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-20
    • 1970-01-01
    • 1970-01-01
    • 2010-09-15
    • 1970-01-01
    • 2011-12-31
    相关资源
    最近更新 更多