Hi, I would like some help with my latest project. What I need to do is somehow compare two worksheets in the same workbook. Both worksheets are the same layout however could contain different records. Both could have well over 15000 records and roughly 60 columns.
(Note you need to run setup script to get the key column setup)
I need first to loop through my key column which is on sheets(1) Column A find the same value on sheets(2) column A if the same is not found then move the entire row to sheets(3) column B and Label sheets(3) column A “Not In Sheets(2)” I then need to Loop through and check Sheets(2) column A with Sheets(1) column (A) if the same is not found then move that entire row to sheets(3) columns B and label sheets(3) column A “Not In Sheets(1)” Next comes a harder task I then need to again loop through each row on sheets(1) column A Find the same on Sheets(2) columns A then loop through each column (B to last column) on sheets(2) checking if the value is the same on the same row and column from sheets(1) if the value is not the same then I need to highlight the cell on both worksheets and continue checking the current row and col then move the entire row from both worksheets to sheets(3) column B and mark sheets(3) Column A “Has changed from Sheets.name”
Not sure if this can be done all together or need separate scripts, here is what I have been trying to work with. Doesn't seem to be working just right, I also included a small workbook with some test data.
Sub Compare()
Setup
NotOnSheet1
NotOnSheet2
End Sub
Sub Setup()
Application.ScreenUpdating = False
For Each wks In Worksheets
wks.Activate
If wks.Index = 3 Then
Sheets(1).Rows(1).Copy wks.Rows(1)
Exit Sub
Else
Lrow = wks.Cells(Rows.Count, 1).End(xlUp).Row
With wks
Columns("A:B").Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
Range("A2").FormulaR1C1 = "=CONCATENATE(RC[2],RC[3])"
Range("A2").AutoFill Destination:=Range("A2:A" & Lrow), Type:=xlFillDefault
Columns("A:A").EntireColumn.AutoFit
Range(Range("A2"), Range("A2").End(xlDown)).Copy
Range("A2").PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Application.CutCopyMode = False
Range("B2:B" & Lrow).FormulaR1C1 = wks.Name
Cells.Select
Cells.EntireColumn.AutoFit
Range("A1").Select
End With
End If
Next wks
Application.ScreenUpdating = True
End Sub
Sub NotOnSheet1()
Dim rng As Range, c As Range, cfind As Range
On Error Resume Next
With Worksheets(1)
Set rng = Range(.Range("A2"), .Range("a2").End(xlDown))
For Each c In rng
With Worksheets(2)
Set cfind = .Columns("A:A").Cells.Find _
(what:=c.Value, lookat:=xlWhole)
If Not cfind Is Nothing Then GoTo line1
c.EntireRow.Copy Worksheets(3).Cells(Rows.Count, "A").End(xlUp).Offset(1, 0)
'c.Copy Worksheets(3).Cells(Rows.Count, "A").End(xlUp).Offset(1, 0)
'c.Offset(0, 1).Copy Worksheets(3).Cells(Rows.Count, "B").End(xlUp).Offset(1, 0)
c.EntireRow.Delete
End With 'sheet 2
line1:
Next c
Application.CutCopyMode = False
End With 'sheet 1
NotOnSheet2
End Sub
Sub NotOnSheet2()
Dim rng As Range, c As Range, cfind As Range
On Error Resume Next
With Worksheets(2)
Set rng = Range(.Range("A2"), .Range("A2").End(xlDown))
For Each c In rng
With Worksheets(1)
Set cfind = .Columns("A:A").Cells.Find _
(what:=c.Value, lookat:=xlWhole)
If Not cfind Is Nothing Then GoTo line1
c.EntireRow.Copy Worksheets(3).Cells(Rows.Count, "A").End(xlUp).Offset(1, 0)
'c.Copy Worksheets(3).Cells(Rows.Count, "A").End(xlUp).Offset(1, 0)
'c.Offset(0, 1).Copy Worksheets(3).Cells(Rows.Count, "B").End(xlUp).Offset(1, 0)
c.EntireRow.Delete
End With 'sheet 1
line1:
Next c
Application.CutCopyMode = False
End With 'sheet 2
End Sub
Any help someone can provide would be great.
Thank You,
Bookmarks