vba 将主工作表中的过滤器应用于多个工作表
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11400850/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Apply filter from a master worksheet to multiple worksheets
提问by J N
I am trying to apply filter from a master worksheet to multiple worksheets in the same workbook.
我正在尝试将主工作表中的过滤器应用于同一工作簿中的多个工作表。
eg.
例如。
There are 3 worksheets in the workbook, with following fields on each sheet:
工作簿中有 3 个工作表,每个工作表上有以下字段:
- Country
- 国家
CountryCode, CountryName
国家代码,国家名称
- Sales
- 销售量
CountryCode, SalesAmount
国家代码、销售额
- Inventory
- 存货
CountryCode, InvAmount
CountryCode, InvAmount
When i filter Country sheet by FRA, JPN, and USA, i expect Sales and Inventory sheets will follow the same filter criteria automatically, and display only FRA, JPN, and USA rows.
当我按 FRA、JPN 和 USA 过滤 Country 表时,我希望 Sales 和 Inventory 表将自动遵循相同的过滤条件,并且仅显示 FRA、JPN 和 USA 行。
It doesn't seems to be too complicated, but i have been trying to code in VBA using AdvancedFilter method, for a few days with no luck!
它似乎并不太复杂,但我一直在尝试使用 AdvancedFilter 方法在 VBA 中编码,几天没有运气!
Many thanks for your help!
非常感谢您的帮助!
回答by Alistair Weir
You want something like this I think, insert the range you want to filter in each worksheet where I've put xx
. Replace i, ii and iii
with the country codes for FRA, JPN and USA.
我想你想要这样的东西,在我放置的每个工作表中插入你想要过滤的范围xx
。替换i, ii and iii
为 FRA、JPN 和美国的国家代码。
Please note this is filtering on the country code as opposed to country name as it is the common field.
请注意,这是对国家代码而不是国家名称进行过滤,因为它是公共字段。
Option Explicit
Sub Apply_Filter()
Dim ws As Worksheet
For Each ws In ThisWorkbook.Worksheets
With ws
.Range("xx").AutoFilter Field:=1, Criteria1:=Array("i", _
"ii", "iii"), Operator:=xlFilterValues
End With
Next
End Sub