2017-04-25 171 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 
相關問題