从来自 Table - Excel VBA 的 ComboBox 中删除重复项
Removing Duplicates from ComboBox sourced from Table - Excel VBA
我有一个组合框,它从 table 列填充值。
该列包含多行,包括具有重复值的行。我希望组合框仅包含唯一值。
我用来填充组合框的代码:
Set wb = Workbooks.Open(Filename:=ThisWorkbook.Path & "Data.xlsx")
Set sh = Worksheets("Sheet1")
Set tbl = sh.ListObjects("Table1")
combobox.list = tbl.ListColumns(1).DataBodyRange.Value
我尝试使用 RemoveDuplicates,但总是出现类型不匹配或对象必需错误。
请尝试下一种方式:
Sub ComboUniqueVal()
Dim wb As Workbook, sh As Worksheet, tbl As ListObject, arr, El, dict As Object
Set wb = Workbooks.Open(fileName:=ThisWorkbook.path & "Data.xlsx")
Set sh = wb.Worksheets("Sheet1")
Set tbl = sh.ListObjects("Table1")
Set dict = CreateObject("Scripting.Dictionary")
arr = tbl.ListColumns(1).DataBodyRange.Value 'for faster iteration
For Each El In arr
dict(El) = 1
Next
ComboBox.list = dict.Keys
End Sub
我有一个组合框,它从 table 列填充值。 该列包含多行,包括具有重复值的行。我希望组合框仅包含唯一值。
我用来填充组合框的代码:
Set wb = Workbooks.Open(Filename:=ThisWorkbook.Path & "Data.xlsx")
Set sh = Worksheets("Sheet1")
Set tbl = sh.ListObjects("Table1")
combobox.list = tbl.ListColumns(1).DataBodyRange.Value
我尝试使用 RemoveDuplicates,但总是出现类型不匹配或对象必需错误。
请尝试下一种方式:
Sub ComboUniqueVal()
Dim wb As Workbook, sh As Worksheet, tbl As ListObject, arr, El, dict As Object
Set wb = Workbooks.Open(fileName:=ThisWorkbook.path & "Data.xlsx")
Set sh = wb.Worksheets("Sheet1")
Set tbl = sh.ListObjects("Table1")
Set dict = CreateObject("Scripting.Dictionary")
arr = tbl.ListColumns(1).DataBodyRange.Value 'for faster iteration
For Each El In arr
dict(El) = 1
Next
ComboBox.list = dict.Keys
End Sub