2

请帮助..我需要一个宏来根据行号(行号)删除特定行,例如,如果我想删除第 2、9、20、150 行,如何在宏中完成,请提供宏,我可以在其中复制和粘贴代码中的行号并从模块运行。我在 sheet2 A 列中有行号,它们是要从 sheet1 中删除的行

4

1 回答 1

2

一行:

Rows(4).Delete Shift:=xlUp

对于多行:

Union(Rows(4), Rows(7)).Delete Shift:=xlUp

对于您的特定情况,允许根据源工作表中的行号列表动态删除行。将 SourceWks 更改为存储数字的工作表,并将 deletedWks 更改为将要删除行的工作表。

Dim deleteRows As Range
Dim data() As Variant
Dim i As Double

Dim SourceWks As Worksheet, deleteWks As Worksheet

Set SourceWks = Sheet2
Set deleteWks = Sheet1

    With SourceWks
        data = .Range(.Cells(1, 1), .Cells(1, 1).End(xlDown))
    End With

    Set deleteRows = deleteWks.Rows(data(1, 1))

    For i = 2 To UBound(data, 1)

        Set deleteRows = Union(deleteRows, deleteWks.Rows(data(i, 1)))

    Next i

    deleteRows.Delete Shift:=xlUp
于 2012-12-14T11:52:40.450 回答