简体   繁体   English

使用VBA筛选Excel表

[英]Filtering Excel Table with VBA

I am trying to filter an Excel table automatically, through VBA. 我正在尝试通过VBA自动筛选Excel表。

The table has a number of rows and is currently in B5:N584 of Sheet2. 该表具有许多行,当前位于Sheet2的B5:N584中。 The first column of the table has "Country" as header and that's the column I want to apply the filter to. 该表的第一列具有“国家/地区”作为标题,这是我要将过滤器应用于的列。 I would like the table to filter automatically depending on the country name, inputted by the user. 我希望表格根据用户输入的国家名称自动过滤。 The country name will be on cell B3 of Sheet2, now defined as "=Sheet1!A1" which is where the user inputs the country. 国家名称将位于Sheet2的单元格B3上,现在定义为“ = Sheet1!A1”,这是用户输入国家/地区的位置。

The way I would like it to work is: - The user writes a country name on cell A1 of Sheet1. 我希望它的工作方式是:-用户在Sheet1的单元格A1上写一个国家/地区名称。 - The country is automatically pulled to cell B3 of Sheet2. -该国家/地区会自动拉到Sheet2的单元格B3。 - The Table is filtered, in the first column, according to the content of cell B3 of Sheet2. -根据Sheet2单元格B3的内容,在第一列中对表格进行了过滤。

This is the VBA code I have written: 这是我编写的VBA代码:

    Public Sub Worksheet_Change(ByVal Target As Range)

   If Target.Address = Range("B3").Address Then
       Range("B5:N584").CurrentRegion.AdvancedFilter Action:=xlFilterInPlace, CriteriaRange:=Range("B2:B3")
   End If

End Sub

On cell B2 I have written "Country" just like in the column header. 我在单元格B2上写了“国家”,就像在列标题中一样。

What I have done is almost working. 我所做的几乎可以正常工作。 The only problem is that the filter is not being applied automatically. 唯一的问题是筛选器没有自动应用。 The user-inputted country on cell A1 is pulled to cell B3 of Sheet2 but the filter is not applied until I click on the formula bar of cell B3 and click enter without changing anything - then the filter is applied to the table below. 单元格A1上用户输入的国家/地区被拉到Sheet2的单元格B3中,但是直到我单击单元格B3的编辑栏并单击Enter而不更改任何内容时,该过滤器才被应用-然后将该过滤器应用于下表。

I am wondering what might be preventing the table from automatically detecting there is a new country on cell B2 without be having to click on the cell and pressing ENTER. 我想知道是什么导致表格无法自动检测到单元格B2上有一个新的国家,而不必单击该单元格并按Enter。

Thank you. 谢谢。

Set your worksheet to trigger the code when the user has entered the data in Cell A1 so you detect a change in this cell and trigger the code rather than wait B3 to be changed. 设置工作表以在用户在单元格A1中输入数据时触发代码,以便您检测到此单元格中的更改并触发代码,而不是等待B3更改。

   If Target.Address = Range("A1").Address Then
   Range("B5:N584").CurrentRegion.AdvancedFilter Action:=xlFilterInPlace, CriteriaRange:=Range("B2:B3")

End If 万一

Considering you have two sheets called Sheet1 and Sheet2 and the table is on Sheet2 in the range you specified. 考虑到您有两个名为Sheet1和Sheet2的工作表,并且该表位于Sheet2中您指定的范围内。

Also assuming the following cases... 还假设以下情况...

  1. User inputs the Country name in A1 on Sheet1. 用户在Sheet1的A1中输入国家/地区名称。
  2. On Sheet2 B2 has the header Country same as the header in B5 in the table. 在Sheet2上,B2的标题Country与表中B5的标题相同。
  3. B3 on Sheet2 has a formula =Sheet1!A1 Sheet2上的B3具有公式= Sheet1!A1

Make sure all the above assumptions are correct, remove the change event code from Sheet1 Module and then place the following code on Sheet2 Module. 确保以上所有假设均正确无误,从Sheet1模块中删除更改事件代码,然后将以下代码放在Sheet2模块上。

Private Sub Worksheet_Calculate()
    Sheets("Sheet2").Range("B5").CurrentRegion.AdvancedFilter Action:=xlFilterInPlace, CriteriaRange:=Sheets("Sheet2").Range("B2:B3")
End Sub

Edit: 编辑:

Actually you can remove the sheet reference because the code is on the Sheet2 Module itself. 实际上,您可以删除工作表引用,因为代码位于Sheet2模块本身上。

Private Sub Worksheet_Calculate()
    Range("B5").CurrentRegion.AdvancedFilter Action:=xlFilterInPlace, CriteriaRange:=Range("B2:B3")
End Sub

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM