简体   繁体   English

在Workbook_Open上清除组合框

[英]Clearing ComboBox on Workbook_Open

I have following code in the Workbook_Open event handler: 我在Workbook_Open事件处理程序中有以下代码:

   Sheet1.ComboBox1.Clear
   Sheet1.ComboBox2.Clear

   Set Rng = Sheet2.Range("A3", Sheet2.Cells(Rows.Count, "A").End(xlUp))
   With CreateObject("Scripting.Dictionary")
   For Each cel In Rng
   If Not .exists(cel.Value) Then
        .Add cel.Value, Nothing
    End If

 Sheet1.ComboBox1.List = .keys

My issue is that it is not clearing ComboBox1 on Workbook_Open and shows earlier selected values. 我的问题是,它没有清除Workbook_Open ComboBox1并显示先前选择的值。

I also used following 我也使用以下

     Sheet1.ComboBox1.ListIndex = -1 ' instead of sheet1.combobox1.clear
     Sheet1.ComboBox2.ListIndex = -1 ' instead of sheet1.combobox2.clear

But the problem remains. 但是问题仍然存在。

Can anyone suggest a solution? 谁能提出解决方案? Thanks in advance to. 在此先感谢。

I made some corrections to you code: 我对您的代码做了一些更正:

Option Explicit

Private Sub Workbook_Open()

    Dim Rng As Range
    Dim cel As Range

    Sheet1.ComboBox1.Clear
    Sheet1.ComboBox2.Clear

    Set Rng = Sheet2.Range("A3", Sheet2.Cells(Rows.Count, "A").End(xlUp))

    With CreateObject("Scripting.Dictionary")

        For Each cel In Rng
            If Not .exists(cel.Value) Then
                .Add cel.Value, Nothing
            End If
        Next cel

        Sheet1.ComboBox1.List = .keys

    End With

End Sub

If I manually add an item to ComboBox and save the workbook, close the workbook, then re-open the workbook then the list reverts to a distinct list from Sheet2 . 如果我手动将一个项目添加到ComboBox并保存工作簿,请关闭工作簿,然后重新打开工作簿,然后列表将还原为与Sheet2不同的列表。

Can you try it? 你可以试试看吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM