Excel - VBA从组合框中删除重复项

时间:2015-02-12 15:42:32

标签: excel-vba combobox duplicate-removal vba excel

我正在尝试创建一个子程序来删除组合框中的重复项。当我调用子程序时,我输入一个数字来代替X.我一直收到一个错误,告诉我" Object Required"当我到达子程序。我知道这意味着某些东西没有被正确初始化,但我无法弄清楚如何解决我的问题。任何帮助将不胜感激。谢谢。

Private Sub UserForm_Initialize()

'ComboBox Populate
Dim rngNext As Range
Dim myRange As Range
Dim C As Integer
With Sheets("KEY")
Set rngNext = .Range("B500").End(xlUp).Offset(1, 0)
End With
rngNext.Select
Set myRange = Range("B2", rngNext)

With ComboBox1
For Each rngNext In myRange

If rngNext <> "" Then .AddItem rngNext

Next rngNext
End With

Call RemoveDuplicates(1)
End sub

 Private Sub RemoveDuplicates(X)
'Remove Duplicates

Dim i As Long
Dim j As Long
With "ComboBox" & X
    For i = 0 To .ListCount + 1 'Getting object required error in this line
        For j = .ListCount To (i + 1) Step -1
            If .List(j) = .List(i) Then
                .RemoveItem j
            End If
        Next
    Next
End With
End Sub

最终守则

一切都非常适合删除重复项。

Public allCBoxes As Collection

Private Sub UserForm_Initialize()


Set allCBoxes = New Collection
allCBoxes.Add ComboBox1

'ComboBox Populate
Dim rngNext As Range
Dim myRange As Range
Dim C As Integer
With Sheets("KEY")
Set rngNext = .Range("B500").End(xlUp).Offset(1, 0)
End With
rngNext.Select
Set myRange = Range("B2", rngNext)

With ComboBox1
For Each rngNext In myRange
If rngNext <> "" Then .AddItem rngNext
Next rngNext
End With

Call RemoveDuplicates(1)
End sub

 Private Sub RemoveDuplicates(X)
'Remove Duplicates
Dim i As Long
Dim j As Long
With allCBoxes(X)
    For i = 0 To .ListCount + 1
        For j = .ListCount -1 To (i + 1) Step -1
            If .List(j) = .List(i) Then
                .RemoveItem j
            End If
        Next
    Next
End With
End Sub

3 个答案:

答案 0 :(得分:1)

您收到错误,因为您传递的是字符串,而不是对象。 虽然直觉上你可以这么想:

"ComboBox" & X
例如,如果x = 5,

将成为

ComboBox5

你错了,因为你实际上在构建一个字符串:

"ComboBox5"

显然,如果在String上调用ComboBox对象的方法,系统将提示您“Object Required”。 你不想在VBA中做什么,你不能在运行时定义变量名(即ComboBox & X,即使不是“as string”,也不会引用变量ComboBox5)。为了达到你想要的,我建议创建一个公共收藏:

Dim allCBoxes As Collection

然后在主程序上填充它:

Set allCBoxes = New Collection
allCBoxes.Add ComboBox1
allCboxes.Add ComboBox2
'etc.

最后像这样恢复“Xth”组合框:

With allCBoxes(X)

End With

答案 1 :(得分:1)

如果您想使用string名称引用控件,请使用Controls函数。

如:

With Controls("Combobox" & X)

这可以解决问题吗?

答案 2 :(得分:0)

正如我在上面的评论中所提到的,这是解决潜在问题的另一种方法:需要combobox没有重复值。此方法使用Dictionary对象。

请告诉我您是否可以根据自己的需求进行调整,以及是否有效。

Private Sub UserForm_Initialize()
    Dim oDictionary As Object
    Dim strCellContent As String
    Dim rngComboValues As Range
    Dim rngCell As Range

    Set rngComboValues = Range("A1:A26")
    Set oDictionary = CreateObject("Scripting.Dictionary")

    For Each rngCell In rngComboValues
        strCellContent = rngCell.Value

        If Not oDictionary.exists(strCellContent) Then
            oDictionary.Add strCellContent, 0
        End If
    Next rngCell

    For Each itm In oDictionary.keys
        Me.ComboBox1.AddItem itm
    Next itm

    Set oDictionary = Nothing
End Sub