Excel - VBA 从组合框中删除重复项
Excel - VBA Removing Duplicates from Comboboxes
我正在尝试创建一个子程序来删除组合框中的重复项。当我调用子程序时,我输入了一个数字来代替 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
您收到错误消息,因为您传递的是字符串,而不是对象。
虽然直觉上你可以认为:
"ComboBox" & X
将变为,例如,如果 x = 5,
ComboBox5
你错了,因为你实际上是在构建一个字符串:
"ComboBox5"
并且,很明显,如果您在 String 上调用 ComboBox 对象的方法,系统将提示您 "Object Required"。
你想做的事情在 VBA 中是不可能的,你不能在 运行 时间定义变量名(即 ComboBox & X
,即使不是 "as string",也不会引用变量ComboBox5
)。为了达到你想要的效果,我建议创建一个 public 集合:
Dim allCBoxes As Collection
然后在主程序上填充它:
Set allCBoxes = New Collection
allCBoxes.Add ComboBox1
allCboxes.Add ComboBox2
'etc.
并最终像这样恢复 "Xth" 组合框:
With allCBoxes(X)
End With
如果要使用其 string
名称引用控件,请使用 Controls
函数。
如:
With Controls("Combobox" & X)
这是否解决了问题?
正如我在上面的评论中提到的,这里有一种解决根本问题的不同方法:需要一个没有重复值的 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
我正在尝试创建一个子程序来删除组合框中的重复项。当我调用子程序时,我输入了一个数字来代替 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
您收到错误消息,因为您传递的是字符串,而不是对象。 虽然直觉上你可以认为:
"ComboBox" & X
将变为,例如,如果 x = 5,
ComboBox5
你错了,因为你实际上是在构建一个字符串:
"ComboBox5"
并且,很明显,如果您在 String 上调用 ComboBox 对象的方法,系统将提示您 "Object Required"。
你想做的事情在 VBA 中是不可能的,你不能在 运行 时间定义变量名(即 ComboBox & X
,即使不是 "as string",也不会引用变量ComboBox5
)。为了达到你想要的效果,我建议创建一个 public 集合:
Dim allCBoxes As Collection
然后在主程序上填充它:
Set allCBoxes = New Collection
allCBoxes.Add ComboBox1
allCboxes.Add ComboBox2
'etc.
并最终像这样恢复 "Xth" 组合框:
With allCBoxes(X)
End With
如果要使用其 string
名称引用控件,请使用 Controls
函数。
如:
With Controls("Combobox" & X)
这是否解决了问题?
正如我在上面的评论中提到的,这里有一种解决根本问题的不同方法:需要一个没有重复值的 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