[英]Creating combobox with only one of each value from worksheet VBA Excel
I am trying to create a combobox with contains data from a worksheet. 我正在尝试创建一个包含工作表中数据的组合框。 I have code that does this but what I need is to only show one of every value in a given column. 我有执行此操作的代码,但我需要的是仅显示给定列中每个值之一。 For example in column AI have multiple dogs,cats and fish, what I want the combobox to show is a list of 3 which is dog,cats,fish. 例如,在AI列中有多个狗,猫和鱼,我想要组合框显示的是3个列表,即狗,猫,鱼。 How do I get it to stop showing dog,dog,dog,cat,cat,fish,fish,fish,fish for example. 我如何才能停止显示例如狗,狗,狗,猫,猫,鱼,鱼,鱼,鱼。 Below is the code that I am currently using. 下面是我当前正在使用的代码。
With Worksheets("RuleID")
OriginatingDomainComboBox.List = .Range("A2:A" & .Range("A" & .Rows.Count).End(xlUp).row).value
End With
Any help would be great and if there is anything else you might need to now let me know. 任何帮助都将非常有用,如果您有其他需要,现在请告诉我。
thanks 谢谢
Here is the method to complete this task: 这是完成此任务的方法:
Public Sub loadValues()
Dim lastRow As Long
Dim rng As Excel.Range
Dim rawData As Variant
Dim columnItems() As String
Dim arraySize As Integer
Dim i As Long
Dim uniqueItems() As Variant
'-------------------------------------------------------------------
'Find data range
With Worksheets(1)
lastRow = .Cells(.Rows.Count, 1).End(xlUp).Row
Set rng = .Range(.Cells(1, 1), .Cells(lastRow, 1))
End With
'Get data from worksheet.
rawData = rng '<-- for better performance we assign all the data to
' Variant array and we iterate through this array
' later instead of iterating through worksheet's cells
'Convert rawData array to 1D array of strings.
arraySize = UBound(rawData, 1) - LBound(rawData, 1) + 1
ReDim columnItems(1 To arraySize)
For i = LBound(columnItems) To UBound(columnItems)
columnItems(i) = rawData(i, 1)
Next i
'Get unique values from [columnItems] array by using function [uniqueValues].
uniqueItems = uniqueValues(columnItems)
'Assign array of unique values as a list to ComboBox.
cmbTest.List = uniqueItems
End Sub
In order to make this method work properly, you need to include the function to get unique values from the given array . 为了使此方法正常工作,您需要包括从给定array中获取唯一值的函数 。
Further to @mielk's answer, you could also use a dictionary to accomplish what you're after. 除了@mielk的答案,您还可以使用词典来完成您想要做的事情。
The below uses the 'Microsoft Scripting Runtime' reference. 下面使用“ Microsoft脚本运行时”参考。 Remember to enable this within Tools -> References. 请记住在工具->参考中启用此功能。
Option Explicit
Sub populateUF()
Dim dict As Scripting.Dictionary, myItem As Variant
Dim lrow As Long, i As Long
Dim myValues() As Variant
Set dict = New Scripting.Dictionary
lrow = Cells(Rows.Count, 1).End(xlUp).Row
myValues = Range(Cells(2, 1), Cells(lrow, 1))
For i = 1 To UBound(myValues, 1)
If Not dict.Exists("Item" & myValues(i, 1)) Then
dict.Item("Item" & myValues(i, 1)) = myValues(i, 1)
End If
Next i
For Each myItem In dict
UserForm1.ComboBox1.AddItem dict.Item(myItem)
Next myItem
UserForm1.Show
End Sub
We are using the .Exists
method to assess whether or not the value from the array has previously been added to the dictionary. 我们正在使用.Exists
方法评估数组中的值是否以前已添加到字典中。 New values are added to the dictionary, resulting in only unique values from the array being assigned. 新值将添加到字典中,从而仅分配数组中的唯一值。 We then iterate through the dictionary using the for each
statement, assigning values to the combobox. 然后,我们使用for each
语句遍历字典,将值分配给组合框。
For further reading on dictionaries, see the documentation here , and in more detail here . 有关词典的更多信息,请参见此处的文档,以及此处的详细信息 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.