需要帮助将字母排序添加到组合框

时间:2018-07-20 22:07:55

标签: excel-vba

我在Excel用户窗体中有一个组合框,希望按字母顺序排序。我不知道如何添加此功能,不胜感激。这是我的VBA:

Private Sub Userform_Initialize()
    ' Sets range for ComboBox list
        Dim rng As Range, r As Range
        Set rng = Sheet1.Range("H2:H65536")

        For Each r In rng
            AddUnique r.value
        Next r
    End Sub


Sub AddUnique(value As Variant)
        Dim i As Integer
        Dim inList As Boolean

        inList = False
        With Me.ComboBox1
            For i = 0 To Me.ComboBox1.ListCount - 1
                If Me.ComboBox1.List(i) = value Then
                    inList = True
                    Exit For
                End If
            Next i

            If Not inList Then
                .AddItem value
            End If
        End With
    End Sub

1 个答案:

答案 0 :(得分:0)

我的建议是使用Dictionary创建一个仅包含您范围内唯一值的集合,然后对其进行排序,然后再将项目添加到组合框中。

  

如果还没有这样做,请通过转到“工具”菜单,将引用库添加到项目中,然后选择“引用”。向下滚动列表,找到“ Microsoft脚本运行时” 并进行检查。

然后,简单地循环遍历所有条目-仅在尚不存在的情况下添加它。我从ExcelMastery中取消了一个排序例程。然后将项目添加到您的组合框中。

Option Explicit

Private Sub Userform_Initialize()
    ' Sets range for ComboBox list
    Dim rng As Range, r As Range
    Set rng = Sheet1.Range("H2:H65536")

    '--- create a dictionary of the items that will be in
    '    the combobox
    Dim uniqueEntries As Object
    Set uniqueEntries = New Scripting.Dictionary
    For Each r In rng
        '--- all dictionary keys must be a string
        Dim keyString As String
        If IsNumeric(r) Then
            keyString = CStr(r)
        Else
            keyString = r
        End If
        If Not uniqueEntries.exists(keyString) Then
            uniqueEntries.Add CStr(keyString), r
        End If
    Next r
    Set uniqueEntries = SortDictionaryByKey(uniqueEntries)
    CreateComboboxList uniqueEntries
End Sub

Private Sub CreateComboboxList(ByRef dictList As Scripting.Dictionary)
    Dim key As Variant
    For Each key In dictList.keys
        Debug.Print "Adding " & key
        'Me.combobox1.AddItem key
    Next key
End Sub

'------------------------------------------------------------------
'--- you should put this in a module outside of your userform code
Public Function SortDictionaryByKey(dict As Object, _
                                    Optional sortorder As XlSortOrder = xlAscending) As Object
    '--- from ExcelMastery
    '    https://excelmacromastery.com/vba-dictionary/#Sorting_by_keys
    Dim arrList As Object
    Set arrList = CreateObject("System.Collections.ArrayList")

    ' Put keys in an ArrayList
    Dim key As Variant, coll As New Collection
    For Each key In dict
        arrList.Add key
    Next key

    ' Sort the keys
    arrList.Sort

    ' For descending order, reverse
    If sortorder = xlDescending Then
        arrList.Reverse
    End If

    ' Create new dictionary
    Dim dictNew As Object
    Set dictNew = CreateObject("Scripting.Dictionary")

    ' Read through the sorted keys and add to new dictionary
    For Each key In arrList
        dictNew.Add key, dict(key)
    Next key

    ' Clean up
    Set arrList = Nothing
    Set dict = Nothing

    ' Return the new dictionary
    Set SortDictionaryByKey = dictNew

End Function