我在excel工作表1中有一些activex组合框和vb代码。但是,每次打开工作簿后,都需要打开代码窗口并运行代码以激活组合框。打开工作簿后,有什么方法可以自动激活并运行属于组合框的工作表1中的代码?
我尝试研究其他论坛/问题,但找不到任何解决方案。 此工作簿中的sheet1.combobox1.activate代码也不起作用。 T
先谢谢您。以下是工作表1中需要激活的代码。
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
答案 0 :(得分:0)
您可以在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
答案 1 :(得分:0)
实际上是在修改workbook_open子对象时找到了解决方案。
通过在子workbook_open()下插入Call Sheet1.ComboBox1_Click
。由于某种原因,触发一个activex足以激活其他activex元素。