I have a sheet1 (sh1) where I have a country name in say (A2) and a direction in (B2).
I would like to find a row on sheet2 (sh2) where columnĀ A contains the same city name and columnĀ B contains the same district and copy that entire row next to the row on sh1 that was matching. Then I would like to loop through all rows on sh1 and find the matching rows on sh2 and copy it in the same manner.
It might seem I am duplicating data, but the matched row on sh2 contains other information I'd like to copy to sh1.
To illustrate:
On Sheet1: Column A Column B (header) (header) San Diego South New York North Chicago East On Sheet2: Column A Column B (header) (header) Chicago East San Diego South New York North
The loop would first check for San Diego, then New York, then Chicago and so on until the end of the column.
Here is my code:
Sub Matchcountry()
Dim sh1 As Worksheet
Dim sh2 As Worksheet
Dim r As Range
Set sh1 = Sheets("Sheet1")
Set sh2 = Sheets("Sheet2")
r = lastrow = sh1.Range("A" & Rows.Count) + 2.End(xlUp).Row
For x = 1 To r
If sh1.Range("A" & x) = sh2.Range("A" & x) And sh1.Range("B" & x) = sh1.Range("A" & x) & sh2.Range("B" & x) Then
sh1.Range("A" & x).EntireRow.Copy Destination:=sh2.Range("C" & x)
x = x + 1
Next x
End Sub