Vba 使用多个非连续单元格值筛选另一工作表中的数据

Vba 使用多个非连续单元格值筛选另一工作表中的数据,vba,excel,autofilter,Vba,Excel,Autofilter,在过去的几天里,我四处寻找这个问题的答案,但没有找到任何与我的问题相关的东西。我希望这里有人能帮我/给我指出正确的方向 基本上,我有一个商店列表和一个客户列表(每个客户访问过的商店)在一个工作簿中的两张不同的表格中,包括一对多关系。我希望能够通过选择门店列表中的门店来动态筛选客户列表,尽管到目前为止,我仅使用以下代码按一个值(门店)进行了筛选: Private Sub Worksheet_SelectionChange(ByVal Target As Range) If ActiveCell.C

在过去的几天里,我四处寻找这个问题的答案,但没有找到任何与我的问题相关的东西。我希望这里有人能帮我/给我指出正确的方向

基本上,我有一个商店列表和一个客户列表(每个客户访问过的商店)在一个工作簿中的两张不同的表格中,包括一对多关系。我希望能够通过选择门店列表中的门店来动态筛选客户列表,尽管到目前为止,我仅使用以下代码按一个值(门店)进行了筛选:

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
当然,这只是在需要选择一家商店时的一种解决方案。如果我需要对单元格进行非连续选择,我将如何处理


任何帮助都将不胜感激

我的方法是处理多个选择。代码如下所示(TBL客户是您的“表1”):

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