使用多个不连续的单元格值过滤另一个工作表中的数据

我已经四处寻找过去几天来find答案,但还没有find任何涉及我的查询的所有方面。 我希望这里的某个人能帮助我/指引我正确的方向!

基本上,我在一个工作簿中的两张不同的工作表中有一个商店列表和一个客户列表(每个客户已经访问过的商店),包括一对多的关系。 我希望能够通过select商店列表中的商店来dynamic地过滤客户列表,尽pipe目前为止只能使用以下代码过滤一个值(商店):

Private Sub Worksheet_SelectionChange(ByVal Target As Range) If ActiveCell.Column = 1 Then Sheet2.ListObjects("Table1").Range.AutoFilter Field:=1, Criteria1:=ActiveCell.Value Sheet2.Activate End If End Sub 

当然,这当然只是一个商店需要被select的解决scheme。 我需要做一个非连续的细胞select,我将如何解决这个问题?

任何帮助将不胜感激!

我的方法是处理多个select。 代码如下所示(TblCustomer是您的“Table1”):

 Private Sub Worksheet_SelectionChange(ByVal Target As Range) Dim rgSel As Range, rgCell As Range Dim cellsFound As Integer Dim filters() As String Set rgSel = Selection cellsFound = 0 For Each rgCell In rgSel If rgCell.Column = 1 Then cellsFound = cellsFound + 1 ReDim Preserve filters(cellsFound) filters(cellsFound - 1) = rgCell End If Next rgCell If cellsFound > 0 Then Sheet2.ListObjects("TblCustomers").Range.AutoFilter Field:=1, Criteria1:=filters, Operator:=xlFilterValues 'you may need to select the customer sheet manually after you made your multiple selection, 'otherwise you'll just jump to it avery time you change the selection 'Sheet2.Activate End If End Sub