基于另一个工作表删除工作表上的行的宏

时间:2016-06-26 12:50:28

标签: excel vba excel-vba

我有一个宏来合并另一张纸上的值,并且根据这些值,它必须返回第一张纸并删除。

表格就像这样,如果G2上的值(Manter a linha),它获取F2上的行数,然后去删除行的预览。 否则,转到I2,并做同样的事情。 感谢您的帮助和时间。

Sheet

到目前为止,我有这个:

Sub Delete() 

    Range("G2").Select
    Do Until IsEmpty(ActiveCell)
    Range("G" & Rows.Count).Select
    If Range("G" & 2).Value = ("<<<Manter a linha") Then
    Sheets("Controle Estoque Fixo").Select
    Rows("2:5").Select
    Selection.EntireRow.Delete
    End If
    Loop

编辑:

Dim r1 As Range, c As Range
    Dim s As String
    Dim v As String
    Dim k As String
    Dim t As String
    k = "1"
    Set r1 = Range(Cells(2, "H"), Cells(Rows.Count, "H").End(xlUp))
    v = Sheets("Analise de Estoque").Cells(2, "G").Value
    For Each c In r1
        If c.Text = ("<<<Manter a linha") Then
        Sheets("Controle Estoque Fixo").Select
        t = (v - 1)

       Rows(t).Select.Clear


        End If
    Next
End Sub

现在我可以返回并选择包含该行的单元格的值,我要保留,所以我在此之前添加“ - 1”进行选择,但我尝试添加乞讨而不会work(尝试将T添加为字符串并将put = 1)

1 个答案:

答案 0 :(得分:1)

您需要构建范围并一次删除所有行。

Sub DeleteMatches()
    Dim r1 As Range, c As Range
    Dim s As String

    Set r1 = Range(Cells(2, "G"), Cells(Rows.Count, "G").End(xlUp))

    For Each c In r1
        If c = "<<<Manter a linha" Then
            If Len(s) Then s = s & ","
            s = s & "A" & c.Offset(0, -1)
        End If
    Next

    If Len(s) Then
        s = Left(s, Len(s) - 1)
        Sheets("Controle Estoque Fixo").Range(s).EntireRow.Delete
    End If

End Sub

如果您只想清除行而不删除它们,那么您可以按照自己的方式进行操作。

Sub DeleteMatches2()
    Dim r1 As Range, c As Range
    Dim t As String

    With Sheets("Analise de Estoque")
        Set r1 = .Range(.Cells(2, "H"), .Cells(Rows.Count, "H").End(xlUp))
    End With

    For Each c In r1
        If c.Text = "<<<Manter a linha" Then
            Sheets("Controle Estoque Fixo").Select
            t = c.Offset(0, -1)
            Rows(t).ClearContents
        End If
    Next
End Sub

Sub DeleteMatches3()
    Dim r1 As Range, c As Range
    Dim i As Long, LastRow As Long
    Dim t As String

    With Sheets("Analise de Estoque")
        LastRow = .Cells(Rows.Count, "H").End(xlUp)
        For i = 2 To LastRow
            If .Cells(i, "G").Text = "<<<Manter a linha" Then
                t = .Cells(i, "F").Text
                Sheets("Controle Estoque Fixo").Rows(t).ClearContents
            End If
        Next

    End With
End Sub

请记住,当您删除行时,您必须从最后一行转到第一行

    For i = LastRow To 2 Step - 1

    Next
相关问题