簡體   English   中英

在 Excel 的用戶表單上多次使用唯一公式

[英]Using the Unique formula multiple times on User form in Excel

我創建了一個填充表格的用戶表單,我希望這個表單基本上將表格中已經存在的信息顯示為列表。 所以要做到這一點,我有一個獨特的列表,用戶表單可以從中獲取數據。 下面的VBA是我用過的。

Private Sub Userform_Initialise()

Dim v, e
With Sheets("dropdowns").Range("I2:I500")
    v = .Value
End With
 
With CreateObject("scripting.dictionary")
    .comparemode = 1
For Each e In v
    If Not .exists(e) Then .Add e, Nothing
Next

If .Count Then Me.ComboBox1.List = Application.Transpose(.keys)
End With

此代碼適用於 1 ComboBox。 我想對其他唯一列表中的另外 2 個組合框重復上述內容。

需要在上面的代碼中進行哪些更改才能使其正常工作?

您可以創建一個子例程,用一個 Range 的值填充一個 ComboBox 並調用此例程 3 次。 您可以看到填充的邏輯沒有改變,唯一的區別是您將范圍和 comboBox 作為參數傳遞。 只需根據您的需要調整組合框的范圍和名稱。

Private Sub UserForm_Initialize()    
    With ThisWorkbook.Sheets("dropdowns")
        FillCombo .Range("I2:A500"), Me.ComboBox1
        FillCombo .Range("J2:J500"), Me.ComboBox2
        FillCombo .Range("K2:K500"), Me.ComboBox3
    End With            
End Sub

Sub FillCombo(r As Range, combobox As Control)
    Dim v, e
    v = r.Value

    With CreateObject("scripting.dictionary")
        .comparemode = 1
        For Each e In v
            If Not .exists(e) Then .Add e, Nothing
        Next
        If .Count Then combobox.List = Application.Transpose(.keys)
    End With
End Sub

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM