Excel Vba宏根据单元格值和列名称删除行或列

时间:2017-03-14 21:42:43

标签: excel vba excel-vba

可以更改以下内容以执行以下操作:根据列名称和单元格值删除行?或多列名称和单元格?示例是:我想在名为“Status”的列中删除任何值为“Complete”的内容,但我可能还想在名为“Second Status”的列中删除任何“Complete”但不删除该行,如果“完成了“在列中”ColumnA“以及多张表格......我知道这要求很多......

希望有意义,提前ty!

$("#div").load("content.php", {}, function(){alert(1)});

1 个答案:

答案 0 :(得分:0)

.AutoFilter方法提供快速复制或删除数据子集。

Option Explicit

Sub Macro1()
    Dim vCOLs As Variant

    With ActiveSheet
        If .AutoFilterMode Then .AutoFilterMode = False
        vCOLs = Array(Application.Match("Status", .Rows(1), 0), _
                      Application.Match("Second Status", .Rows(1), 0), _
                      1)
        With .Cells(1, 1).CurrentRegion
            'do not delete rows with 'Completed' in first column
            .AutoFilter field:=vCOLs(2), Criteria1:="<>Completed"
            'delete rows with 'Complete' in Status column
            .AutoFilter field:=vCOLs(0), Criteria1:="Complete"
            With .Resize(.Rows.Count - 1, .Columns.Count).Offset(1, 0)
                If CBool(Application.Subtotal(103, .Cells)) Then
                    .Cells.EntireRow.Delete
                End If
            End With
            .AutoFilter field:=vCOLs(0)
            'delete rows with 'Complete' in Second Status column
            .AutoFilter field:=vCOLs(1), Criteria1:="Complete"
            With .Resize(.Rows.Count - 1, .Columns.Count).Offset(1, 0)
                If CBool(Application.Subtotal(103, .Cells)) Then
                    .Cells.EntireRow.Delete
                End If
            End With
        End With
        If .AutoFilterMode Then .AutoFilterMode = False
    End With
End Sub