使用 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

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

Filtering Worksheet Data Using VBA

excelexcel-vbaexcel-2007vba

提问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

  1. Manually, Autofilter the column, add a criteria of <50%, then delete the results
  2. 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
    
  1. 手动,自动过滤列,添加 <50% 的条件,然后删除结果
  2. 在代码中运行相同的方法如下

    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