在VBA中使用函数自动筛选“Tab 1”中的数据,并将结果自动筛选到“Tab 2”中。具体实现方法是使用Autofilter方法,结合Range和Criteria1参数来实现。代码示例如下:
Sub AutofilterData()
Dim ws1 As Worksheet, ws2 As Worksheet
Dim rngFilter As Range, rngCriteria As Range
Set ws1 = ThisWorkbook.Worksheets("Tab 1")
Set ws2 = ThisWorkbook.Worksheets("Tab 2")
'设置要筛选的数据范围(第一列到第三列)
Set rngFilter = ws1.Range("A1:C" & ws1.Cells(Rows.Count, 1).End(xlUp).Row)
'设置筛选条件(假设第一列为"Apple")
Set rngCriteria = ws2.Range("A1:A1")
'筛选数据并将结果自动填充到“Tab2”中
rngFilter.AutoFilter Field:=1, Criteria1:=rngCriteria.Value
rngFilter.Copy Destination:=ws2.Range("A2:C2")
rngFilter.AutoFilter
End Sub