使用 VBA 过滤工作表数据
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8303211/
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
Filtering Worksheet Data Using VBA
提问by AME
Using: Excel 2007.
使用:Excel 2007。
Problem: A spreadsheet contains many columns and rows of data. One column, called "Probability", contains percentage values between 0% and 100%. How does one write a macro that conditionally removes a row giving it has a "Probability" value is less than50%?
问题:电子表格包含多列和多行数据。一列称为“概率”,包含介于 0% 和 100% 之间的百分比值。如何编写一个有条件地删除一行的宏,使其“概率”值小于50%?
Update: This cannot simply be done by recording a macro, as the percentage values will vary with each new data upload.
更新:这不能简单地通过录制宏来完成,因为百分比值会随着每次新数据上传而变化。
Thanks!
谢谢!
采纳答案by brettdj
If for example your Probability data was in column A of the active sheet, you could use Autofilter
例如,如果您的概率数据位于活动表的 A 列中,则可以使用 Autofilter
- Manually, Autofilter the column, add a criteria of <50%, then delete the results
Run the same approach in code as below
Sub QuickCull() Application.ScreenUpdating = False With ActiveSheet .AutoFilterMode = False .Columns("A").AutoFilter Field:=1, Criteria1:="<50%" .AutoFilter.Range.Offset(1, 0).EntireRow.Delete .AutoFilterMode = False End With Application.ScreenUpdating = True End Sub
- 手动,自动过滤列,添加 <50% 的条件,然后删除结果
在代码中运行相同的方法如下
Sub QuickCull() Application.ScreenUpdating = False With ActiveSheet .AutoFilterMode = False .Columns("A").AutoFilter Field:=1, Criteria1:="<50%" .AutoFilter.Range.Offset(1, 0).EntireRow.Delete .AutoFilterMode = False End With Application.ScreenUpdating = True End Sub