我如何比较两个数据范围然后复制并粘贴不匹配的范围

时间:2019-01-29 18:48:54

标签: excel vba

该站点对使我的代码正常工作非常有帮助,我快完成了,但是如果不满足条件,我将努力返回范围的值。基本上,如果比较的值不匹配,则将其加上五个单元格复制到右侧,并粘贴到从H2开始的“输出”图纸范围的下一个可用行中。我有代码的第一部分,但在这里真的很茫然。我已尽力将代码正确排序。我必须重复“对于每个”吗?

Dim WS1 As Worksheet: Set WS1 = ThisWorkbook.Sheets("Increments")
Dim WS2 As Worksheet: Set WS2 = ThisWorkbook.Sheets("Output")

Dim LR1 As Long, LR2 As Long, WS1_Cell As Range, WS2_Cell As Range

LR1 = WS1.Range("S" & WS1.Rows.Count).End(xlUp).Row
LR2 = WS2.Range("H" & WS2.Rows.Count).End(xlUp).Row

For Each WS1_Cell In WS1.Range("S1:S" & LR1)
    For Each WS2_Cell In WS2.Range("H1:H" & LR2)
        If WS1_Cell = WS2_Cell Then
            WS2_Cell.Offset(, 5).Value = WS1_Cell.Offset(, 5).Value
        Else
            WS2_Cell.Offset(1, 1).Value = WS1_Cell.Offset(1, 1).Value
        End If
    Next WS2_Cell
Next WS1_Cell

例如,如果sheet1上的s2不等于sheet2上h2:H100范围内的任何值,则复制范围s2:x2并粘贴到sheet2上H的最后一行下方的第一个可用单元格上

0 个答案:

没有答案