简体   繁体   English

比较两个工作表并复制行数据

[英]Compare Two Worksheets and Copy Row Data

I would like to compare two worksheets in one workbook. 我想在一个工作簿中比较两个工作表。 Some pseudocode : 一些伪代码

If Cell A on Worksheet1 = Cell A on Worksheet2 And 如果工作表1上的单元格A =工作表2上的单元格A
If Cell F on Worksheet1 <> Cell F on Worksheet2 Then 如果工作表1上的单元格F <>工作表2上的单元格F
Copy Row from Worksheet2 over the Row on Worksheet1 Else 将工作表2中的行复制到其他工作表1中的行上
If Cell A on Worksheet1 <> Cell A on Worksheet2 Then 如果工作表1上的单元格A>工作表2上的单元格A
Copy Row from Worksheet2 to next blank Row on Worksheet1 将行从Worksheet2复制到Worksheet1上的下一个空白行

This is what I have so far: 这是我到目前为止的内容:

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("Sheet1")
Set sh2 = Worksheets("Sheet2")

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, "A").Value = sh2.Cells(j, "A").Value And sh1.Cells(i, "F").Value <> sh2.Cells(j, "F").Value Then
            sh1.Cells(i, "F").Value = sh2.Cells(j, "F").Value
        End If
    Next j
Next i

End Sub 结束子

Try this: 尝试这个:

Sub CopyCells()
    Dim sh1 As Worksheet, sh2 As Worksheet
    Dim i As Long, j As Long, lastrow1 As Long, lastrow2 As Long, counter As Long

    Set sh1 = Worksheets("Sheet1")
    Set sh2 = Worksheets("Sheet2")

    lastrow1 = sh1.Cells(Rows.Count, "A").End(xlUp).Row
    lastrow2 = sh2.Cells(Rows.Count, "A").End(xlUp).Row
    counter = 1

    For i = 1 To lastrow1
        For j = 1 To lastrow2
            If sh1.Range("A" & i) = sh2.Range("A" & j) And sh1.Range("F" & i) <> sh2.Range("F" & j) Then
                sh2.Range("A" & j).EntireRow.Copy Destination:=sh1.Range("A" & i)
            ElseIf sh1.Range("A" & i) <> sh2.Range("A" & j) Then
                sh2.Range("A" & j).EntireRow.Copy Destination:=sh1.Range("A" & lastrow1 + counter)
                counter = counter + 1
            End If
        Next j
    Next i
End Sub

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 比较两个EXCEL工作表并将相似的数据复制到第三张工作表上 - Compare two EXCEL worksheets and copy similar data onto a third sheet 在工作表之间比较和复制数据 - Compare and copy data between worksheets 用于比较两个工作表中的两列并复制公共数据的 VBA 代码 - VBA code to compare two columns from two worksheets and copy the common data 我想比较两个工作表中的数据,如果有匹配项,则复制一个单元格 - I'd like to compare data in two worksheets and copy a cell if there's a match 在两个Excel工作表上匹配列并复制数据 - Match Columns on two excel worksheets and copy data 比较两个工作表,找到第一个匹配项并粘贴,然后删除源行 - Compare two worksheets, find first match and paste, then delete source row 如何复制所有工作表中的最后一行数据并将其粘贴到一个工作表中 - How to copy last row of data in all worksheets and paste it into one worksheet 比较同一文件的两个工作表中的数据并突出显示单元格 - Compare data in two worksheets of same file and highlight the cell 根据第a列和第d列的值比较来自两个工作表的数据 - compare data from two worksheets based on values of column a column d 如何比较(不同)工作表上的两个(数据透视表)表数据? - How to compare two (pivot) tables data on (different) worksheets?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM