2017-04-25 109 views
0

在belwo编码的帮助下,我可以从组合框中删除重复的项目,但它不会反映项目的升序。我想按照升序来反映组合框中的所有项目。 请协助。组合框显示项目的升序

ComboBox37.Clear 
With CreateObject("Scripting.Dictionary") 
    For Each rCell In wksSource.Range("M6", wksSource.Cells(Rows.Count, "M").End(xlUp)) 
      If Not .exists(rCell.Value) Then 
       .Add rCell.Value, Nothing 
      End If 
    Next rCell 
    ComboBox37.List = .keys 
End With 

回答

0

你有什么有一个好的开始,你只需要立即对它们进行排序一旦他们所有加载:

ComboBox37.Clear 
With CreateObject("Scripting.Dictionary") 
    For Each rCell In wksSource.Range("M6", wksSource.Cells(Rows.Count, "M").End(xlUp)) 
      If Not .exists(rCell.Value) Then 
       .Add rCell.Value, Nothing 
      End If 
    Next rCell 
    ComboBox37.List = .keys 
End With 

With ComboBox37 
    For a = 0 To .ListCount - 1 
     For b = a To .ListCount - 1 
      If .List(b) < .List(a) Then 
       c = .List(a) 
       .List(a) = .List(b) 
       .List(b) = c 
      End If 
     Next 
    Next 
End With 
0

ArrayList是这样的情况下,另一种有用的数据结构,该方法支持Contains方法(类似于Dictionary.Exists方法)以及Sort方法。如果您不想操作(排序)工作表数据本身,这将非常有用:

Dim list As Object 
Set list = CreateObject("System.Collections.ArrayList") 
Set rng = wksSource.Range("M6", wksSource.Cells(Rows.Count, "M").End(xlUp)) 
ComboBox37.Clear 
For Each rCell In rng.Cells 
    If Not list.Contains(rCell.Value) Then list.Add (rCell.Value) 
Next rCell 
Dim v 
'Sort the ArrayList object: 
list.Sort 
ComboBox37.list = list.ToArray() 
Set list = Nothing 
相关问题