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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 16:48:57  来源:igfitidea点击:

Apply filter from a master worksheet to multiple worksheets

excelexcel-vbavba

提问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 个工作表,每个工作表上有以下字段:

  1. Country
  1. 国家

CountryCode, CountryName

国家代码,国家名称

  1. Sales
  1. 销售量

CountryCode, SalesAmount

国家代码、销售额

  1. Inventory
  1. 存货

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 iiiwith 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