问题
Okay so I managed to work out this code somehow with help from many of the coding experts here. I need to create a macro that compares data in two worksheets.
In both of my worksheets, there is a column named "eRequest ID", I have to copy the rows of records that DO NOT have an "eRequest ID" in BOTH FILES.
The code i worked out now copies recrods that have an "eRequest ID" in EITHER FILES. So logically speaking i have to "negate" the IF condition in my code below, but I have no idea how to do it as I'm a total beginner at coding, VBA included.
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
Forgive my poor formatting.. I'm very new to stackoverflow as well.
One more thing, I just realized that this code only copies rows from Sheets("JULY15Release_Master Inventory")
It doesnt copy rows from Sheets("JULY15Release_Dev status")
even if there is only one "eRequest ID" for that data.
回答1:
Your "eRequest ID" are located in both sheets in column A so I compare this column. If there is a match in both sheets the loop exists. If there is no match then the row get copied to "mismatch". loop for both sheets.
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
来源:https://stackoverflow.com/questions/30050485/excel-vba-compare-column-data-copy-row