如何一起自动过滤多列?

How to auto filter many columns together?

我正在处理 Excel sheet,我想在其中同时过滤许多列。例如,我有从 B 到 G 的列,每列的第一个单元格上都有数据验证列表到 select。当我们 select 列更改数据时,我编写了代码。当我在下一列应用相同的数据时,我得到了第二列的数据,但它删除了第一列的过滤器。我想要的是一起处理所有列的过滤器,

这是我的代码:

Private Sub Worksheet_Change(ByVal Target As Range)
Dim lastrow As Long
lastrow = Cells(Rows.Count, "C").End(xlUp).Row
With Me
If Not Intersect(Target, .Range("B1")) Is Nothing Then
If Target.Value <> "" Then
.AutoFilterMode = False
.Range("B2:B" & lastrow).AutoFilter Field:=1, Criteria1:=Target.Value
End If
End If
If Not Intersect(Target, .Range("C1")) Is Nothing Then
If Target.Value <> "" Then
.AutoFilterMode = False
.Range("C2:C" & lastrow).AutoFilter Field:=1, Criteria1:=Target.Value
End If
End If
End With
End Sub

我需要做什么才能使两个过滤器协同工作。

我想你想要这个:

Private Sub Worksheet_Change(ByVal Target As Range)
Dim lastrow As Long
Dim rCriteria As Range
Dim i As Long
lastrow = Cells(Rows.Count, "C").End(xlUp).Row
With Me
  Set rCriteria = .Range("B1:C1")
  If Not Intersect(Target, rCriteria) Is Nothing Then
    .AutoFilterMode = False
    For i = 1 To rCriteria.Count
      If rCriteria(i) <> "" Then
        .Range("B2:C" & lastrow).AutoFilter Field:=i, Criteria1:=rCriteria(i)
      End If
    Next
  End If
End With
End Sub