Searching in 2nd sheet for values in first

Closed
Toff - Nov 11, 2011 at 02:33 AM
TrowaD Posts 2921 Registration date Sunday September 12, 2010 Status Moderator Last seen December 27, 2022 - Nov 14, 2011 at 09:27 AM
Hello,

I am looking for a macro to search data in col A of first sheet in col A of 2nd sheet and whereever there is a match copy the data in ColB of 2nd sheet in to one of the empty cells in ColA. Can someone please help.

1 response

TrowaD Posts 2921 Registration date Sunday September 12, 2010 Status Moderator Last seen December 27, 2022 552
Nov 14, 2011 at 09:27 AM
Hi Toff,

Try this code:
Sub test()
Dim lRow As Integer
Dim x As Integer
Dim y As Integer

lRow = Sheets("Sheet1").Range("A" & Rows.Count).End(xlUp).Row
y = Sheets("Sheet2").Range("A" & Rows.Count).End(xlUp).Row

For Each cell In Sheets("Sheet1").Range("A1:A" & lRow)
x = 1
Do
If cell.Value = Sheets("Sheet2").Range("A" & x).Value Then
Sheets("Sheet2").Range("B" & x).Copy Destination:=Sheets("Sheet2").Range("A" & Rows.Count).End(xlUp).Offset(1, 0)
End If
x = x + 1
Loop Until x > y
Next
End Sub

Hopefully I understood you correctly.
It's always best to provide a small example of how your sheet looks like and how you would like your sheet to look like after the code.

Best regards,
Trowa
0