Excel 我试图创建一个用户表单,从单元格填充组合框选项。如何仅从单元格区域提取唯一值?
到目前为止,我已经将单元格中的所有值填充到我的组合框中:Excel 我试图创建一个用户表单,从单元格填充组合框选项。如何仅从单元格区域提取唯一值?,excel,vba,Excel,Vba,到目前为止,我已经将单元格中的所有值填充到我的组合框中: Private Sub UserForm_Initialize() 'Extracts Locations From Cells Dim ws As Worksheet Set ws = Worksheets("B") Dim Location As Range For Each Location In ws.Range("E7:E100") With Me.Combobox
Private Sub UserForm_Initialize()
'Extracts Locations From Cells
Dim ws As Worksheet
Set ws = Worksheets("B")
Dim Location As Range
For Each Location In ws.Range("E7:E100")
With Me.Combobox
.AddItem Location.Value
End With
Next Location
End Sub
我在另一个论坛上找到了答案
例如,使用演示的
Scripting.Dictionary
。感谢您的回复。我一直在尝试实现这一点,但我对VBA非常陌生,似乎无法将其合并到我的代码中。我是否需要调暗另一个范围,然后使用唯一值从这个新范围中为每个范围执行一个操作?您应该能够将字典键添加到组合框中,而无需使用新范围。
Private Sub UserForm_Activate()
Application.ScreenUpdating = False
Dim rng As Range
Dim Dn As Range
Dim Dic As Object
Set rng = Range(Sheets("Subscription").Range("U2"), Sheets("Subscription").Range("U" & Rows.Count).End(xlUp))
Set Dic = CreateObject("scripting.dictionary")
Dic.CompareMode = vbTextCompare
For Each Dn In rng
If Not Dn = vbNullString Then Dic(Dn.Value) = Empty
Next
With UserForm1.ComboBox1
.Columncount = 2
.RowSource = ""
.List = Dic.Keys
.ListIndex = 0
End With