基于其他工作表删除工作表上的行的macros

我有一个macros,在另一个工作表上合并值,并基于这些值,它必须回到第一个工作表并删除。

这张纸是这样的,如果G2上的数值(Manter a linha),那么就得到F2上的行数,然后删除行的预览。 否则,去I2,并做同样的事情。 感谢您的帮助和时间。

我到目前为止:

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 

现在我可以返回并select包含该行的单元格的值,我想保留,所以我添加一个“ – 1”之前select,但我试图添加乞讨,不会工作(尝试添加T作为一个string,并把= 1)

你需要build立你的范围,并一次删除所有的行。

 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