排除组合框中的重复值

Excluding duplicate values in combobox

有人可以告诉我如何添加代码以排除组合框中的重复值吗?我已经排除了一些在列中具有特定值的行(例如,当工作已经完成时) 如果无法在 1 代码中执行,则在组合框列表创建后删除重复项也可以。不过这个我也想不通怎么办。

这是我的组合框列表的代码。它位于 Userform1_initialize 部分内。

Dim LastRow As Long
Dim aCell As Range

Set ws = Sheets("Requests")

With ws

    LastRow = .Cells(.Rows.Count, "B").End(xlUp).Row

    For Each aCell In .Range("B3:B" & LastRow)
        'discard rows in dropdown for ease of use
        If aCell.Value <> "" And aCell.Offset(0, 25).Value = "" And aCell.Offset(0, 22).Value <> "on hold" And aCell.Offset(0, 22).Value <> "cancelled" Then
    
            Me.ComboBox2.AddItem aCell.Value

        End If

    Next
End With

您可以使用脚本字典(假设您不在 mac 上):

Dim dict, ws As Worksheet
Dim LastRow As Long
Dim aCell As Range

Set ws = Sheets("Requests")
Set dict = CreateObject("scripting.dictionary")

With ws

    LastRow = .Cells(.Rows.Count, "B").End(xlUp).Row

    For Each aCell In .Range("B3:B" & LastRow)

        'had to guess here since your Q is missing operators ...
        If aCell.Value <> "" And aCell.Offset(0, 25).Value = "" And _
           aCell.Offset(0, 22).Value <> "on hold" And _
           aCell.Offset(0, 22).Value <> "cancelled" Then

            If Not dict.exists(aCell.Value) Then '? new value ?
                Me.ComboBox2.AddItem aCell.Value
                dict(aCell.Value) = True 'add this value
            End If

        End If

    Next
End With