Hi Lillian,
I hope that what I have done will do the trick for you. However, make sure
that you back up your workbook before installing and running it just in case
it does not do what you expect.
I have also included a second macro (see comments at top of macro) which
will perform in the reverse just in case you have any data on the first sheet
that does not appear on the second sheet.
Sub Compare_Copy_1()
'This macro as per you request
Dim rngSht1 As Range
Dim rngSht2 As Range
Dim foundCell As Range
With Sheets("Sheet1")
Set rngSht1 = Range(.Cells(2, 1), _
.Cells(Rows.Count, 1).End(xlUp))
End With
With Sheets("Sheet2")
Set rngSht2 = Range(.Cells(2, 1), _
.Cells(Rows.Count, 1).End(xlUp))
End With
For Each c In rngSht2
Set foundCell = rngSht1.Find(What:=c.Value, _
LookIn:=xlFormulas, _
LookAt:=xlWhole, _
SearchOrder:=xlByColumns, _
SearchDirection:=xlNext, _
MatchCase:=False, _
SearchFormat:=False)
If Not foundCell Is Nothing Then
Range(foundCell, rngSht1.Cells(foundCell.Row, 3)).Copy _
Destination:=c.Offset(0, 5)
Else
'Following line just in case data left in cells
Range(c.Offset(0, 5), c.Offset(0, 8)) = ""
End If
Next c
End Sub
Sub Compare_Copy_2()
'This macro works in reverse and copies matching
'data to sheet1 in case you have any data in sheet1
'that is not on sheet2
Dim rngSht2 As Range
Dim rngSht1 As Range
Dim foundCell As Range
With Sheets("Sheet2")
Set rngSht2 = Range(.Cells(2, 1), _
.Cells(Rows.Count, 1).End(xlUp))
End With
With Sheets("Sheet1")
Set rngSht1 = Range(.Cells(2, 1), _
.Cells(Rows.Count, 1).End(xlUp))
End With
For Each c In rngSht1
Set foundCell = rngSht2.Find(What:=c.Value, _
LookIn:=xlFormulas, _
LookAt:=xlWhole, _
SearchOrder:=xlByColumns, _
SearchDirection:=xlNext, _
MatchCase:=False, _
SearchFormat:=False)
If Not foundCell Is Nothing Then
Range(foundCell, rngSht2.Cells(foundCell.Row, 3)).Copy _
Destination:=c.Offset(0, 5)
Else
'Following line just in case data left in cells
Range(c.Offset(0, 5), c.Offset(0, 8)) = ""
End If
Next c
End Sub
Regards,
OssieMac