将一系列单元格从一个 sheet 复制并粘贴到另一个单元格,然后清除原始单元格中的数据

Copy and paste a range of cells from one sheet to another then clear data from orignal cells

我有以下运行良好的代码,但是我想做的是修改代码以复制将清除的数据到 Sheet2 以进一步调查继续清除原始 sheet .代码本身所做的就是查看 G 和 H。如果 H 小于 G,则它会清除 A:J 的内容。我现在想要的是在满足条件的情况下仍然清除内容,但是我也想要复制到 Sheet2 的单元格副本。

Sub ClearRange()

Dim myLastRow As Long
Dim i As Long

Application.ScreenUpdating = False

' Find last row
myLastRow = Cells(Rows.Count, "G").End(xlUp).Row

' Loop through range
For i = 5 To myLastRow
If Cells(i, "H").Value < Cells(i, "G").Value Then Range(Cells(i, "A"), Cells(i, "J")).ClearContents
Next i

Application.ScreenUpdating = True

End Sub

提前感谢您提供的任何帮助。

您可以只更新这部分代码:

' Loop through range
For i = 5 To myLastRow
    If Cells(i, "H").Value < Cells(i, "G").Value Then
        With Range(Cells(i, "A"), Cells(i, "J"))
            .Copy
            Sheets("Sheet2").Paste Destination:=Sheets("Sheet2").Range("A" & i)
            .ClearContents
        End With
    End If
Next