【问题标题】:Excel Activex combobox activate when excel workbook is opened打开 Excel 工作簿时激活 Excel Activex 组合框
【发布时间】:2019-03-15 17:51:07
【问题描述】:

我在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

【问题讨论】:

    标签: excel vba activex


    【解决方案1】:

    您可以在使用 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
    

    【讨论】:

    • oxwilder 这似乎运作良好,但我有一堆下拉菜单,它们相互链接。我不确定它会如何工作,即下拉更改值能否触发另一个下拉列表中的不同列表?
    • 但是我找到了一种触发activex下拉菜单的方法。通过在子 workbook_open() 下插入“Call Sheet1.ComboBox1_Click”。由于某种原因,触发一个activex就足以激活其他activex元素。
    【解决方案2】:

    其实在修改workbook_open sub的时候找到了解决办法。

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

    【讨论】:

      猜你喜欢
      • 2023-03-24
      • 1970-01-01
      • 1970-01-01
      • 2014-09-12
      • 1970-01-01
      • 2019-02-12
      • 1970-01-01
      • 1970-01-01
      • 2017-01-16
      相关资源
      最近更新 更多