简体   繁体   English

输出列表框到多个单元格以用于自动过滤器数组

[英]Output listbox to multiple cells to use for autofilter array

So I have a multiselect listbox that users choose regions they want to filter in a dataset. 因此,我有一个多选列表框,用户可以选择要在数据集中过滤的区域。

I currently output those selections into a single cell using this sub: 我目前使用以下子程序将这些选择输出到单个单元格中:

Dim listItems As String, i As Long
With RegionListBox
    For i = 0 To .ListCount - 1
        If .Selected(i) Then listItems = listItems & .List(i) & ", "
    Next i
End With

Range("AA1") = Left(listItems, Len(listItems) - 2)

At the end of the macro users run to update their workbook, I want to apply a filter using the values that I have output from the listbox. 在宏用户运行更新他们的工作簿的最后时,我想使用从列表框中输出的值来应用过滤器。 Currently this is how I do it: 目前,这是我的操作方式:

Worksheets("4. Booking Summary").Range("$A$1:$G$1000" _
).AutoFilter Field:=4, Criteria1:=(Range("$AA$1").Value),` Operator:=xlFilterValues

Unfortunately this doesn't work, as if users have selected multiple choices, the output in AA1 will be formatted like "text, text, text" . 不幸的是,这不起作用,因为用户选择了多个选项,AA1中的输出将被格式化为“ text,text,text” When it tries to apply this filter, it applies a text filter with that full string. 当尝试应用此过滤器时,它将应用具有该完整字符串的文本过滤器。 For it to have the desired function it would need to filter using an array with each selected item. 为了使其具有所需的功能,将需要使用每个选定项的数组进行过滤。

I'm a bit stuck on how to achieve this. 我对如何实现这一目标有些困惑。 My thought was I need to output each listbox selection to a single cell, then set the autofilter criteria to an array referencing those cells. 我的想法是我需要将每个列表框选择输出到单个单元格,然后将自动过滤条件设置为引用这些单元格的数组。 I'm just not sure how to do this. 我只是不确定如何执行此操作。 Happy for any suggestions. 很高兴提出任何建议。

您可以使用Split()创建一个数组:

Criteria1:=Split(Range("$AA$1").Value,", ")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM