从筛选列表(VBA)中选择列表框

时间:2017-07-17 09:24:22

标签: vba listbox

我是VBA的新手,非常感谢有关当前问题的任何指导。我正在尝试创建一个列表框,该列表框是根据用户以前的选择填充的。我已经使用了基于两个单元格“A2”和“B2”的DoFilter来减少行数(我希望填充ListBox1)

Sub DoFilter()

Dim rCrit1 As Range, rCrit2 As Range, rRng1 As Range, rRng2 As Range

With Application
    .EnableEvents = False
    .ScreenUpdating = False
End With

Set rCrit1 = Sheets("QuestionBank").Range("A2")
Set rCrit2 = Sheets("QuestionBank").Range("B2")

Set rRng1 = Sheets("QuestionBank").Range("A5:AA2300")
Set rRng2 = Sheets("QuestionBank").Range("A6:AA2300")

With rRng1
    .AutoFilter Field:=1, Criteria1:=rCrit1.Value, Operator:=xlOr
    .AutoFilter Field:=2, Criteria1:=rCrit2.Value

End With

    Application.EnableEvents = True
End Sub

然后我想要这个过滤范围来填充我的listbox1。但是,就目前而言,它使用所有数据填充它,而不仅仅是过滤后的数据。

Private Sub Worksheet_Activate()
    Dim myCell As Range
    Dim rngItems As Range
    Set rngItems = Sheets("QuestionBank").Range("ItemList") 'ItemList refers to a column of data which populates the ListBox    

Me.ListBox1.MultiSelect = fmMultiSelectSingle
Me.ListBox1.Value = ""
Me.ListBox1.MultiSelect = fmMultiSelectMulti
Me.ListBox1.Value = ""


    Me.ListBox1.Clear
    Me.ListBox2.Clear

    With Me.ListBox1
        .LinkedCell = ""
        .ListFillRange = ""
        For Each myCell In rngItems.Cells
            If Trim(myCell) <> "" Then
                .AddItem myCell.Value
            End If
        Next myCell
    End With

    Me.ListBox1.MultiSelect = fmMultiSelectMulti
    Me.ListBox2.MultiSelect = fmMultiSelectMulti

End Sub

提前感谢您的支持(由于我还处于学习VBA的早期阶段,因此非常感谢您对答案的解释)

由于

1 个答案:

答案 0 :(得分:0)

管理修复它....对于那些需要答案的人,我把下面的内容放在DoFilter的末尾:

 Range("D6").Select
    Range(Selection, Selection.End(xlDown)).SpecialCells(xlCellTypeVisible).Select
Selection.Name = "ItemList"