Vba 自动过滤柱A&;至于具体的单元格标准,请删除。大变化数据

Vba 自动过滤柱A&;至于具体的单元格标准,请删除。大变化数据,vba,excel,Vba,Excel,我下面的代码用于删除相同的数据转储 我想让它适用于不同大小的数据。我只需要对单元格=2的列A进行过滤,在过滤的过程中,对所有大于等于11的数字进行过滤 我不知道如何将其输入到“criterial:=Array(u)部分:查看下面的示例静态数据集的工作代码: 非常感谢!这将使用ActiveSheet 如果列A包含值=2的单元格,它将自动筛选它:col A=2 如果colAS包含值为>10的单元格,它将自动对其进行筛选:col AS>10 删除以START\u DEL\u行开始的所有剩余可见行

我下面的代码用于删除相同的数据转储

我想让它适用于不同大小的数据。我只需要对单元格=2的列A进行过滤,在过滤的过程中,对所有大于等于11的数字进行过滤

我不知道如何将其输入到“criterial:=Array(u)部分:查看下面的示例静态数据集的工作代码:




非常感谢!

这将使用
ActiveSheet

  • 如果列
    A
    包含值
    =2
    的单元格,它将自动筛选它:
    col A=2
  • 如果col
    AS
    包含值为
    >10
    的单元格,它将自动对其进行筛选:
    col AS>10
  • 删除以
    START\u DEL\u行开始的所有剩余可见行

你可以用这个

    With Intersect(ActiveSheet.UsedRange, Range("A:AS")) ' reference its columns A:AS range
        .AutoFilter Field:=1, Criteria1:="2"  ' filter referenced range column 1 cells with "2"
        .AutoFilter Field:=.Columns.Count, Criteria1:=">11"  ' filter referenced range last column with ">11"
        If Application.WorksheetFunction.Subtotal(103, .Resize(, 1)) > 1 Then .Resize(.Rows.Count - 1).Offset(1).SpecialCells(xlCellTypeVisible).EntireRow.Delete 'if any filtered cells other than delete their entire row
    End With
    ActiveSheet.AutoFilterMode = False ' remove autofilter

在.Autofilter中只能使用2个条件;最简单的方法是为
列(45)
使用辅助列。将此基本公式
=AS1>10
放在辅助列中,并复制到最后一行,然后在辅助列
Criteria1:=“True”
向上投票,简单直接,优于辅助列。
Option Explicit

Public Sub FilterAndDelete()
    Const Col1 = 1              'A
    Const Col2 = 45             'AS
    Const Col1_COND = 2         'A criteria
    Const Col2_COND = ">10"     'AS criteria
    Const START_DEL_ROW = 11    'First deleted visible row (to last used row)

    Dim ws As Worksheet, lr As Long, ur As Range, vis As Range, keep As Range

    Set ws = ActiveSheet
    lr = ws.Cells(ws.Rows.Count, Col1).End(xlUp).Row
    Set ur = ws.Range(ws.Cells(1, Col1), ws.Cells(lr, Col2))

    'If col A has cells with val 2
    If WorksheetFunction.CountIf(ur.Columns(Col1), Col1_COND) > 0 Then
        Application.ScreenUpdating = False
        ur.AutoFilter Field:=Col1, Criteria1:=Col1_COND     'Filter col A
        Set vis = ur.SpecialCells(xlCellTypeVisible)

        'If col AS has cells with val > 10
        If WorksheetFunction.CountIf(vis.Columns(Col2), Col2_COND) > 0 Then
            vis.AutoFilter Field:=Col2, Criteria1:=Col2_COND, Operator:=xlAnd
            Set keep = ur.Range(ur.Cells(1, Col1), ur.Cells(START_DEL_ROW - 1, Col1))
            keep.Rows.Hidden = True 'Delete all visible rows (except topof START_DEL_ROW)
            If CBool(Application.Subtotal(103, ur.Cells)) Then
                ur.SpecialCells(xlCellTypeVisible).EntireRow.Delete
            End If
            keep.Rows.Hidden = False
        End If
        ur.AutoFilter
        Application.ScreenUpdating = True
    End If
End Sub
    With Intersect(ActiveSheet.UsedRange, Range("A:AS")) ' reference its columns A:AS range
        .AutoFilter Field:=1, Criteria1:="2"  ' filter referenced range column 1 cells with "2"
        .AutoFilter Field:=.Columns.Count, Criteria1:=">11"  ' filter referenced range last column with ">11"
        If Application.WorksheetFunction.Subtotal(103, .Resize(, 1)) > 1 Then .Resize(.Rows.Count - 1).Offset(1).SpecialCells(xlCellTypeVisible).EntireRow.Delete 'if any filtered cells other than delete their entire row
    End With
    ActiveSheet.AutoFilterMode = False ' remove autofilter