无法设置ListBox类的List属性

时间:2018-07-02 10:53:32

标签: excel-vba vba excel

我有一个代码,它将打开一个源工作簿,然后从特定的列开始,它将仅使用不同的值,并使用它们来填充列表框,供以后用于过滤。该代码适用于两个ListBox,但现在我不得不扩展到第三个ListBo。我使用了相同的代码,但是现在我得到了上面提到的错误,我不明白->我对VBA还是很陌生

这是它在其中打破“ macroSheet.ListBoxes(“列表框5”)。List = .keys“的行。中间块“填充listbox5”是listbox6的复制粘贴。如果我取出这个中间块,代码将正常运行。

我注意到,我用于ListBox 5的F列中的数据不是连续的->我之间有空单元格,并且我认为代码在那儿分解了。有什么提示如何解决吗?过滤掉这些单元格似乎不起作用->我尝试使用(If Not eCell =“ <>”)甚至手动过滤。

Dim i As Long
Dim j As Long
Dim Temp As Variant
Dim srcSheet As Worksheet
Dim srcBook As Workbook
Dim cCell As Range
Dim lbx As ListBox
Dim macroSheet As Worksheet:      Set macroSheet = ThisWorkbook.Worksheets("MACRO")
Dim setSheet As Worksheet:        Set setSheet = ThisWorkbook.Worksheets("Charts")

Set dict1 = CreateObject("Scripting.Dictionary")
Set dict2 = CreateObject("Scripting.Dictionary")
Set dict3 = CreateObject("Scripting.Dictionary")

'Populate ListBox6
macroSheet.ListBoxes("List Box 6").RemoveAllItems
With dict1
    For Each cCell In srcSheet.Range("G2", srcSheet.Cells(Rows.count, "G").End(xlUp))
        If Not .exists(cCell.Value) Then
            .Add cCell.Value, Nothing
        End If
    Next cCell
macroSheet.ListBoxes("List Box 6").List = .keys
End With
'Sort ListBox alphabetically
With macroSheet.ListBoxes("List Box 6")
    For i = 1 To .ListCount - 1
        For j = i + 1 To .ListCount
            If .List(i) > .List(j) Then
                Temp = .List(j)
                .List(j) = .List(i)
                .List(i) = Temp
            End If
        Next j
    Next i
End With

'Populate ListBox5
macroSheet.ListBoxes("List Box 5").RemoveAllItems
With dict2
    For Each cCell In srcSheet.Range("F2", srcSheet.Cells(Rows.count, "F").End(xlUp))
        If Not .exists(cCell.Value) Then
            .Add cCell.Value, Nothing
        End If
    Next cCell
macroSheet.ListBoxes("List Box 5").List = .keys
End With
'Sort ListBox alphabetically
With macroSheet.ListBoxes("List Box 5")
    For i = 1 To .ListCount - 1
        For j = i + 1 To .ListCount
            If .List(i) > .List(j) Then
                Temp = .List(j)
                .List(j) = .List(i)
                .List(i) = Temp
            End If
        Next j
    Next i
End With

'Populate ListBox10
macroSheet.ListBoxes("List Box 10").RemoveAllItems
With dict3
    For Each cCell In srcSheet.Range("L2", srcSheet.Cells(Rows.count, "L").End(xlUp))
        If Not .exists(cCell.Value) Then
            .Add cCell.Value, Nothing
        End If
    Next cCell
macroSheet.ListBoxes("List Box 10").List = .keys
End With

'De-Select all Items in all ListBoxes
For Each lbx In macroSheet.ListBoxes
    For i = 1 To lbx.ListCount
        lbx.Selected(i) = False
    Next i
Next lbx

0 个答案:

没有答案
相关问题