I have two sheets. Sheet 1 contains the following data
Sheet 1:
Column 1 column 2
Hotel A New York
Hotel B Melbourne
And I wish to replace the value in sheet 2 with this value
Sheet 2 is like that :
Column 1 Column 2 Column 3
Name .... .....
..... .... City
.... .... ....
Name .... .....
.... ..... City
My ideal output will be :
Column1 Column 2 Column 3
Hotel A .... .....
..... .... New York
.... .... ....
Hotel B .... .....
.... .... Melbourne
So, I wish to go through a loop in sheet 1
and read the name and city of hotels and go to sheet 2
and find the words Name
and City
and replace them with what I read in sheet 1
. I'm very new in VBA and started my code like that and it even goes to loop. Why is it so?
Sub testLoopPaste()
Dim j, k, L, b As String
Dim i As Long
Dim wb As Workbook
Dim sht1 As Worksheet
Dim sht2 As Worksheet
Set wb = ThisWorkbook
Set sht1 = wb.Sheets("Sheet1")
Set sht2 = wb.Sheets("Sheet2")
j = "Name"
b = "City"
For i = 1 To 2
k = sht1.Range("A" & i)
L = sht1.Range("B" & i)
sht2.Cells.Replace what:=j, replacement:=k, lookat:=xlWhole, MatchCase:=False
sht2.Cells.Replace what:=b, replacement:=L, lookat:=xlWhole, MatchCase:=False
Next i
End Sub
Any tips or guidance is appreciated.