Excel VBA比较列数据复制行

好吧,所以我在这里的许多编码专家的帮助下,设法编写了这个代码。 我需要创build一个比较两个工作表中的数据的macros。

在我的两个工作表中,有一个名为“eRequest ID”的列,我必须复制在两个文件中 没有 “eRequest ID”的logging行。

我现在制定的代码复制了在任何文件中都有“eRequest ID”的logging。 所以从逻辑上说,我必须在下面的代码中“否定”IF条件,但是我不知道该怎么做,因为我是编码方面的初学者,包括VBA。

Sub compareAndCopy() Dim lastRowE As Integer Dim lastRowF As Integer Dim lastRowM As Integer Dim foundTrue As Boolean Application.ScreenUpdating = False lastRowE = Sheets("JULY15Release_Master Inventory").Cells(Sheets("JULY15Release_Master Inventory").Rows.Count, "A").End(xlUp).Row lastRowF = Sheets("JULY15Release_Dev status").Cells(Sheets("JULY15Release_Dev status").Rows.Count, "A").End(xlUp).Row lastRowM = Sheets("Mismatch").Cells(Sheets("Mismatch").Rows.Count, "A").End(xlUp).Row For i = 1 To lastRowE foundTrue = False For j = 1 To lastRowF If Sheets("JULY15Release_Master Inventory").Cells(i, 2).Value = Sheets ("JULY15Release_Dev status").Cells(j, 7).Value Then foundTrue = False Exit For End If Next j If Not foundTrue Then Sheets("JULY15Release_Master Inventory").Rows(i).Copy Destination:= _ Sheets("Mismatch").Rows(lastRowM + 1) lastRowM = lastRowM + 1 End If Next i Application.ScreenUpdating = False End Sub 

原谅我可怜的格式。我也很新的stackoverflow以及。

还有一件事,我刚刚意识到,这个代码只从Sheets("JULY15Release_Master Inventory")复制行Sheets("JULY15Release_Master Inventory") ,即使只有一个“eRequest ID”数据,也不会从Sheets("JULY15Release_Dev status")复制行Sheets("JULY15Release_Dev status")

您的“eRequest ID”位于A列的两张表格中,因此我会比较此列。 如果两张纸都有匹配,则存在循环。 如果没有匹配,则该行被复制到“不匹配”。 循环两张纸。

 Sub compareAndCopy() Dim lastRowMaster As Integer Dim lastRowDev As Integer Dim lastRowMis As Integer Dim foundTrue As Boolean Application.ScreenUpdating = False lastRowMaster = Sheets("JULY15Release_Master Inventory").Cells(Sheets("JULY15Release_Master Inventory").Rows.Count, "A").End(xlUp).Row lastRowDev = Sheets("JULY15Release_Dev status").Cells(Sheets("JULY15Release_Dev status").Rows.Count, "A").End(xlUp).Row lastRowMis = Sheets("Mismatch").Cells(Sheets("Mismatch").Rows.Count, "A").End(xlUp).Row 'start loop Master For i = 2 To lastRowMaster '1 = headers foundTrue = False For j = 2 To lastRowDev If Sheets("JULY15Release_Master Inventory").Cells(i, 1).Value = Sheets("JULY15Release_Dev status").Cells(j, 1).Value Then foundTrue = True Exit For End If Next j If foundTrue = False Then Sheets("JULY15Release_Master Inventory").Rows(i).Copy Destination:= _ Sheets("Mismatch").Rows(lastRowMis + 1) lastRowMis = lastRowMis + 1 End If Next i 'end loop master Sheets("Mismatch").Cells(lastRowMis + 2, 1).Value = "results from Dev" lastRowMis = lastRowMis + 2 'start loop Dev For i = 2 To lastRowDev '1 = headers foundTrue = False For j = 2 To lastRowMaster If Sheets("JULY15Release_Dev status").Cells(i, 1).Value = Sheets("JULY15Release_Master Inventory").Cells(i, 1).Value Then foundTrue = True Exit For End If Next j If foundTrue = False Then Sheets("JULY15Release_Dev status").Rows(i).Copy Destination:= _ Sheets("Mismatch").Rows(lastRowMis + 1) lastRowMis = lastRowMis + 1 End If Next i 'end loop dev Application.ScreenUpdating = False End Sub