簡體   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