Scripting.Dictionary键到Listbox VBA Excel

时间:2013-05-13 05:04:44

标签: excel vba excel-vba listbox scripting.dictionary

我一直在使用excel中的VBA,最近开始使用Scripting.Dictionary对象。直到今天我才遇到任何重大问题。

基本上我试图使用字典的Key值填充列表框,然后再向列表框添加一个值。这导致该值不仅被添加到列表框中,而且还被添加到字典作为键。我试图将dict.keys()数组中的值复制到一个完全独立的数组,但仍然有同样的问题。我认为这是一个byref问题,但还没有找到解决方案。如果有人有任何想法会很棒。

Private Sub Setup_settingLst()
'Set Settings listbox items
    'On Error GoTo ErrorExit
    Dim list_ary() As Variant
    Dim tmp As Variant
    Dim i As Integer

    settingLst.Clear
    settingLst.Value = "-Select Setting-"
    i = 0
    tmp = tmp_dict.Keys()
    If tmp_dict.Count > 1 Then
        ReDim list_ary(0 To tmp_dict.Count)
        For i = 0 To UBound(tmp)
            list_ary(i) = tmp(i)
        Next i
        list_ary(tmp_dict.Count) = "Back"
    Else
        ReDim list_ary(0 To tmp_dict.Count - 1)
        For i = 0 To UBound(tmp)
            list_ary(i) = tmp(i)
        Next i
    End If
    settingLst.List = list_ary
    Erase list_ary
    Exit Sub
ErrorExit:
End Sub

1 个答案:

答案 0 :(得分:1)

这似乎有效

Private Sub UserForm_Click()

    Dim dcValues As Scripting.Dictionary

    Me.ListBox1.Clear
    Set dcValues = FillDictionary

    Me.ListBox1.List = dcValues.Keys
    Me.ListBox1.AddItem "Back"

End Sub