繁体   English   中英

在Workbook_Open上清除组合框

[英]Clearing ComboBox on Workbook_Open

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

我的问题是,它没有清除Workbook_Open ComboBox1并显示先前选择的值。

我也使用以下

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

但是问题仍然存在。

谁能提出解决方案? 在此先感谢。

我对您的代码做了一些更正:

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

如果我手动将一个项目添加到ComboBox并保存工作簿,请关闭工作簿,然后重新打开工作簿,然后列表将还原为与Sheet2不同的列表。

你可以试试看吗?

暂无
暂无

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

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