How to make a list in VBA (not a dictionary)?

后端 未结 1 1362
猫巷女王i
猫巷女王i 2021-01-22 19:08

I\'m not that familiar with VB or VBA, though I am familiar with programming.

I\'m working with Excel 2016 and trying to populate a combobox (that lives on a user form)

相关标签:
1条回答
  • 2021-01-22 19:42

    I'd like to use a data structure that offers a sort function, if one exists.

    Yes exists, for example SortedList from .Net framework can be used.

    SortedList represents a collection of key/value pairs that are sorted by the keys and are accessible by key and by index.

    VBA code example:

    Add reference to C:\Windows\Microsoft.NET\Framework\v4.0.30319\mscorlib.tlb

    Sub SortedListDemo()
        Dim sr As mscorlib.SortedList
        Set sr = New mscorlib.SortedList
        sr.Add "D", "D"
        sr.Add "B", "B"
        sr.Add "A", "A"
        sr.Add "C", "C"
        Dim i As Integer
        For i = 0 To sr.Count - 1
            Debug.Print "Key: " & sr.GetKey(i) & ", Value: " & sr.GetByIndex(i)
        Next i
        Set sr = Nothing
    End Sub
    

    Output

    Key: A, Value: A
    Key: B, Value: B
    Key: C, Value: C
    Key: D, Value: D
    

    Some more information e.g. here. HTH

    0 讨论(0)
提交回复
热议问题