调整每个variables

我遇到了一些我认为相当基本的问题。 有人可以帮忙吗?

如何调整For Each循环中的“单元格”值? 由于我删除了一行,我的“单元格”计数器被抛出,每次删除一行就跳过一行。

我想我可以通过调整计数器来修复它,但是需要replace:Set cell = cell – 1

For Each row In pipelineRange.Rows For Each cell In row.Cells If cell.Value = "EXIT" Then cell.EntireRow.Copy Destination:=Worksheets("EXITS").Range("A" & Sheets("EXITS").Range("B" & Rows.Count).End(xlUp).row + 1) Sheets("PIPELINE TRACKER").Activate cell.EntireRow.Delete Set cell = cell - 1 ElseIf cell.Value = 1 Then cell.EntireRow.Copy Destination:=Worksheets("COMPLETED").Range("A" & Sheets("COMPLETED").Range("B" & Rows.Count).End(xlUp).row + 1) Sheets("PIPELINE TRACKER").Activate cell.EntireRow.Delete Set cell = cell - 1 End If Next cell Next row 

删除时总是向后退避免。 例如:

 For X = Range("A" & Rows.count).end(xlup).row to 2 step -1 If Range("A" & X).Text = "Delete Me" then rows(X).delete Next 

我们知道这里范围的第一行: pipelineRange.Row我们也知道在这里范围的行数pipelineRange.Rows.Count如果我们将它们加在一起,然后减1,我们将得到范围的最后一行

也许这样的事情

 Sub Something() Dim X As Long For X = pipelineRange.Row + pipelineRange.Rows.Count - 1 To pipelineRange.Row Step -1 For Each cell In Rows(X).Cells If cell.Value = "EXIT" Then cell.EntireRow.Copy Destination:=Worksheets("EXITS").Range("A" & Sheets("EXITS").Range("B" & Rows.Count).End(xlUp).Row + 1) Sheets("PIPELINE TRACKER").Activate cell.EntireRow.Delete 'You could change this for rows(X).delete if you like ElseIf cell.Value = 1 Then cell.EntireRow.Copy Destination:=Worksheets("COMPLETED").Range("A" & Sheets("COMPLETED").Range("B" & Rows.Count).End(xlUp).Row + 1) Sheets("PIPELINE TRACKER").Activate cell.EntireRow.Delete 'You could change this for rows(X).delete if you like End If Next Next End Sub