Excel VBA 条件过滤器
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11727297/
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
Excel VBA Conditional Filter
提问by jrad
simple VBA question. I need a VBA subroutine that will filter my data based on a condition (in this case, if what's in column C equals 11-Jun-12 [41071 in numeric form]) without looping through the whole data set. I've looked a bit at filtering on the Internet but nothing seems to be quite what I want (or I just don't really understand what's going on). To be clear, here's an example of what I want:
简单的 VBA 问题。我需要一个 VBA 子例程,它将根据条件过滤我的数据(在这种情况下,如果 C 列中的内容等于 11-Jun-12 [41071 以数字形式]),而无需遍历整个数据集。我在互联网上看了一些过滤,但似乎没有什么是我想要的(或者我只是不明白发生了什么)。说清楚,这是我想要的一个例子:
I want http://imgur.com/qebVv
to go to http://imgur.com/zDncq.
Thanks!
谢谢!
回答by Jon Kelly
Assuming that the spreadsheet is set up as it appears in your screenshot here is what you can do
假设电子表格已按照屏幕截图中的显示进行设置,那么您可以执行以下操作
Sub DateFilter()
'hide dialogs
Application.ScreenUpdating = False
'filter for records that have June 11, 2012 in column 3
ActiveSheet.Cells.AutoFilter Field:=3, Criteria1:="6/11/2012"
'deletes all matching rows
ActiveSheet.AutoFilter.Range.Delete
Application.ScreenUpdating = True
End Sub
回答by HymanOrangeLantern
Hyman Radcliffe,
Hyman·雷德克里夫
Do you mean a simple autofilter, for example:
你的意思是一个简单的自动过滤器,例如:
Sub SimpleColumnDateFilter1()
' Quick Recorded Macro
' Select a Column
' Activate Autofilter
' For a range C1 through the rest of C
' Autofilter such that the column will display dates not equal to 11/15/2012
Columns("C:C").Select
Selection.AutoFilter
ActiveSheet.Range("C:C").AutoFilter Field:=1, Criteria1:= _
"<>11/15/2012", Operator:=xlAnd
End Sub