基于另一个单元格的 VBA 过滤器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/45419664/
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-12 13:00:56  来源:igfitidea点击:

VBA Filter based on another cell

excelvbaexcel-vba

提问by av abhishiek

I am VBA beginner and was trying to filter the data based a cell value,after googling around a bit I have written a code which works

我是 VBA 初学者,试图过滤基于单元格值的数据,在谷歌搜索之后我写了一个有效的代码

Sub FilterDepartment_Sales()
    Sheet6.Activate
    Sheet6.Cells.Select
    Selection.AutoFilter
    Selection.AutoFilter Field:=12, Criteria1:=Sheet8.Range("B3").Value
End Sub

I had to assign this macro to object to run , the data was not getting automatically refreshed wen I change the cell value in B3, how to make it auto refresh whenevr I change the value in the B3

我必须将此宏分配给要运行的对象,数据没有自动刷新我更改了 B3 中的单元格值,如何使其在更改 B3 中的值时自动刷新

Also in the above code when I try to do the following it gives me an wrror

同样在上面的代码中,当我尝试执行以下操作时,它给了我一个错误

Sub FilterDepartment_Sales()
    Sheet6.Activate
    'Sheet6.Cells.Select
    'Selection.AutoFilter
    'Selection.AutoFilter Field:=12, Criteria1:=Sheet8.Range("B3").Value

    Sheet6.Cells.Select.AutoFilter Field:=12, Criteria1:=Sheet8.Range("B3").Value
End Sub

I get an "Object not found error" , any reason on why I can't condense the code like that

我收到一个“找不到对象的错误”,任何关于为什么我不能像这样压缩代码的原因

Base on the answer I had modified the code

根据我修改了代码的答案

Private Sub Worksheet_SelectionChange(ByVal Target As Range)


    Sheet6.Activate
    Sheet6.Cells.Select
    Selection.AutoFilter
    Selection.AutoFilter Field:=12, Criteria1:=Sheet8.Range("B3").Value




End Sub

But now when I change the value in B3 nothing is happening , do I need to add anything ?

但是现在当我更改 B3 中的值时什么也没有发生,我是否需要添加任何内容?

采纳答案by Micha? Turczyn

If you want to run a code every time you change something in a sheet, you have to place the code within a method, which handles event of changing something.

如果您想在每次更改工作表中的某些内容时运行代码,您必须将代码放置在一个方法中,该方法处理更改某些内容的事件。

https://i.stack.imgur.com/sZQqH.png

https://i.stack.imgur.com/sZQqH.png

Here is the place, where you have to find change event, on the left you have to select Sheet1, then chose Worksheetfromd ropdown list, the right one will contain all events. Change event will be raised on every change, so you'll need to ,,filter" this and handle only situation when the particular cell was changed:

在这里,您必须找到更改事件,在左侧您必须选择Sheet1,然后Worksheet从下拉列表中选择,右侧将包含所有事件。Change 事件将在每次更改时引发,因此您需要 ,,filter" 并仅处理特定单元格更改时的情况:

Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Row = 2 And Target.Column = 3 Then
    Sheet6.Cells.AutoFilter Field:=12, Criteria1:=Sheet8.Range("B3").Value
End If
End Sub

In the example we watch for changes only in cell B3.

在这个例子中,我们只观察 cell 中的变化B3