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

从筛选列表中选择列表框(VBA),vba,listbox,Vba,Listbox,我是VBA新手,希望您能就当前问题提供指导。我试图创建一个列表框,该列表框根据用户以前的选择填充。我使用了基于两个单元格“A2”和“B2”的DoFilter来减少行数(我想填充ListBox1) 然后,我希望这个过滤范围填充我的listbox1。但是,就目前的情况而言,它使用所有数据填充它,而不仅仅是过滤数据 Private Sub Worksheet_Activate() Dim myCell As Range Dim rngItems As Range Set rng

我是VBA新手,希望您能就当前问题提供指导。我试图创建一个列表框,该列表框根据用户以前的选择填充。我使用了基于两个单元格“A2”和“B2”的DoFilter来减少行数(我想填充ListBox1)

然后,我希望这个过滤范围填充我的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
Private子工作表\u Activate()
暗淡的迈塞尔山脉
Dim rngItems As范围
Set rngItems=Sheets(“问题库”)。Range(“ItemList”)“ItemList”是指填充列表框的一列数据
Me.ListBox1.MultiSelect=fmMultiSelectSingle
Me.ListBox1.Value=“”
Me.ListBox1.MultiSelect=fmMultiSelectMulti
Me.ListBox1.Value=“”
Me.ListBox1.Clear
Me.ListBox2.Clear
和我一起
.LinkedCell=“”
.ListFillRange=“”
对于rngItems.细胞中的每个菌丝体
如果修剪(迈塞尔)”,则
.AddItem myCell.Value
如果结束
下一个迈塞尔
以
Me.ListBox1.MultiSelect=fmMultiSelectMulti
Me.ListBox2.MultiSelect=fmMultiSelectMulti
端接头
提前感谢您的支持(非常感谢您对答案的解释,因为我仍处于学习VBA的早期阶段)


谢谢

设法解决了。。。。对于那些需要答案的人,我将以下内容放在DoFilter的末尾:

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