Excel excel 工作簿打开时激活 Activex 组合框

Excel Activex combobox activate when excel workbook is opened

我在excel中有一些activex组合框和vb代码 sheet 1.但是每次打开工作簿后,我都需要打开代码window和 运行 激活组合框的代码。有什么方法可以在我打开工作簿后自动激活运行属于combobox的sheet1中的代码吗?

我尝试查看其他 forums/questions 但找不到任何解决方案。 sheet1.combobox1.activate 本工作簿中的代码也不起作用。 T

以下是sheet1中需要激活的代码。

Public oDictionary As Object

Private Sub ComboBox1_Click()
    Dim r As Range
    Dim list As Object

    Set oDictionary = CreateObject("Scripting.Dictionary")

    With Sheet2
        For Each r In .Range("C11", .Cells(.Rows.Count, "c").End(xlUp))  

            If Not oDictionary.Exists(r.Text) Then                       
                Set list = CreateObject("System.Collections.ArrayList")  

                oDictionary.Add r.Text, list
            End If

            If Not oDictionary(r.Text).Contains(r.Offset(0, 1).Value) Then  
                oDictionary(r.Text).Add r.Offset(0, 1).Value                
            End If

        Next
    End With

    ComboBox1.list = oDictionary.Keys  'Display the list in combobox 1
End Sub

您可以 运行 在使用 sub workbook_open() 自动打开的工作簿上编写代码,然后在该事件而不是单击事件上创建组合框:

Dim r As Range
Dim list As Object
Dim rng As Range
Dim rng1 As Range


Set oDictionary = CreateObject("Scripting.Dictionary")

With Sheet2
    Set rng1 = .Range("C11", .Cells(.Rows.Count, "c").End(xlUp))
    For Each r In rng1.Cells

        If Not oDictionary.Exists(r.Text) Then
            Set list = CreateObject("System.Collections.ArrayList")

            oDictionary.Add r.Text, list
        End If

        If Not oDictionary(r.Text).Contains(r.Offset(0, 1).Value) Then
            oDictionary(r.Text).Add r.Offset(0, 1).Value
        End If

    Next
End With

Set rng = Worksheets(1).Range("c11") 'where the dropdown list will go
    With rng
        Set box1 = Worksheets(1).DropDowns.Add(.Left, .Top, .Width, .Height)
            With box1
              .Name = "ComboBoxIn" & rng.Address(False, False)
              .list = oDictionary.Keys
            End With
     End With
end sub

在修改 workbook_open 子程序时实际上找到了解决方案。

通过在子 workbook_open() 下插入 Call Sheet1.ComboBox1_Click。由于某种原因,触发一个activex足以激活其他activex元素。