Compare and copy data between worksheets

前端 未结 1 1065
暖寄归人
暖寄归人 2020-12-20 09:00

Here\'s what I would like to do:

  • IF
    • cell H of worksheet A = cell E of worksheet B (contain words) and
    • ce
相关标签:
1条回答
  • 2020-12-20 09:32

    Update You need two loops for what you want to do. This new subroutine works for any row. Just be careful of multiple matches because it will take only the last match:

    Sub CopyCells()
        Dim sh1 As Worksheet, sh2 As Worksheet
        Dim j As Long, i As Long, lastrow1 As Long, lastrow2 As Long
        Set sh1 = Worksheets("Worksheet A")
        Set sh2 = Worksheets("Worksheet B")
    
        lastrow1 = sh1.Cells(Rows.Count, "A").End(xlUp).Row
        lastrow2 = sh2.Cells(Rows.Count, "A").End(xlUp).Row
    
        For i = 2 To lastrow1
            For j = 1 To lastrow2
                If sh1.Cells(i, "H").Value = sh2.Cells(j, "E").Value And _
                    sh1.Cells(i, "J").Value = sh2.Cells(j, "H").Value And _
                    sh1.Cells(i, "K").Value = sh2.Cells(j, "I").Value Then
                    sh1.Cells(i, "L").Value = sh2.Cells(j, "O").Value
                End If
            Next j
        Next i
    End Sub
    
    0 讨论(0)
提交回复
热议问题