将唯一列值添加到按字母顺序排序的用户窗体组合框

时间:2018-10-04 23:51:07

标签: vba

嗨,我正在尝试将三列中的唯一值添加到三个不同的组合框。我也希望使用数组和for循环,因此每列它循环3次。这三个组合框名为cmbTruck;。 cmbSource;和cmbDest,并希望从第2列中输入值; 16;从第二行到最后一个条目分别为16和16。

Dim cel As Range
Dim rngItems As Range
Dim oDictionary As Object
Dim arr As Variant
Dim cmbOptions As Variant
Dim i as long
Dim lrow As Long


arr = array(2, 16, 16)
cmbOptions = Array(cmbTruck, cmbSource, cmbDest)

lrow = Range("A1").End(xlDown).Row

For i = LBound(arr) to UBound(arr)

With cmbOptions(i)
    Set oDictionary = createobject("Scripting.Dictionary") 
    Set rngItems = ThisWorkbook.ActiveSheet.Range(Cells(2, arr(i)), Cells(lrow, 2))

    For Each cel In rngItems
        If oDictionary.exists(cel.Value) Then
        'Do Nothing
        Else
            oDictionary.Add cel.Value, 0
            .AddItem cel.Value
        End If
    Next cel
End With

Next i 

我在Line17上收到错误“ 424”对象。我认为这是由cmbOptions数组和带有cmbOptions的with循环行无法识别为comboboxname引起的。我能在这方面得到帮助,将不胜感激。

Jae

0 个答案:

没有答案