![如何使用 VBA 根据单元格值进行过滤?](https://linux22.com/image/1574893/%E5%A6%82%E4%BD%95%E4%BD%BF%E7%94%A8%20VBA%20%E6%A0%B9%E6%8D%AE%E5%8D%95%E5%85%83%E6%A0%BC%E5%80%BC%E8%BF%9B%E8%A1%8C%E8%BF%87%E6%BB%A4%EF%BC%9F.png)
我的工作簿中有两张工作表。我使用以下 VBA 代码来过滤数据:
Sub apply_autofilter_across_worksheets()
'Updateby Extendoffice 20160623
Dim xWs As Worksheet
On Error Resume Next
For Each xWs In Worksheets
xWs.Range("A4").AutoFilter 1, "=001"
Next
End Sub
现在我想用单元格F1中的值代替001。我该如何修改代码?
答案1
这很简单,试试看:
Sub apply_autofilter_across_worksheets()
' Updateby Extendoffice 20160623
Dim xWs As Worksheet
On Error Resume Next
For Each xWs In Worksheets
xWs.Range("A4").AutoFilter FIELD:=1, CRITERIA1:=xWs.range("F1").value
Next
End Sub
干杯