宏正在删除我想要保留的内容

时间:2016-06-26 20:31:58

标签: excel vba excel-vba

我有一个宏,但它删除了我要保留的行,基于它必须保留的数字(行数),它可以移动到另一个工作表,但我尝试的一切它只是移动最后一个匹配或删除我想要保留的每一行。 What happens

Sub KeepRows()
    Dim x As Range, u As Range
    Dim y As String

    With Sheets("Sheetwithnumberrow")
        Set x = .Range(.Cells(2, "H"), .Cells(Rows.Count, "H").End(xlUp))
    End With

    For Each u In x
        If u.Text = "keep row" Then
            Sheets("SheetthatIwannaClean").Select
            y = u.Offset(0, -1)
            Rows(y).ClearContents
        End If
    Next
End Sub
编辑:我把正在发生的事情的图像,我试图移动到另一张表而不是清理内容,总线只移动最后一行。

Product ROW Instruction ROW Instruction
1          6    keep row    6   keep row
2          15   keep row    15  keep row
3          18   Try Next    17  keep row
4          24   keep row    24  keep row
5          28   keep row    28  keep row

逻辑就像thig,它在行,如果指令要保持,必须保持,并转到下一行。但问题是,代码是删除行。

1 个答案:

答案 0 :(得分:0)

Sub KeepRows()
    Dim x As Range, u As Range
    Dim y As String

    With Sheets("Sheetwithnumberrow")
        Set x = .Range(.Cells(2, "H"), .Cells(.Rows.Count, "H").End(xlUp))
    End With

    For Each u In x
        If u.Text <> "keep row" Then Sheets("SheetthatIwannaClean").Rows(u.Offset(0, -1)).EntireRow.ClearContents
    Next u
End Sub