title: Vlookup VBA Code in Excel, not copying data in the right cell (Need just a small fix in my code)
body: <p>I need a little help with fixing the VBA code that I have written.
  Question:</p>
  
  <p>I have an excel file with 3 columns on sheet1 ("Name", "ID" and "Name Data"): "Name Data" is a blank column.</p>
  
  <pre><code>Name        ID      Name Data
  Albert      4235 
  John        4237 
  Robert      4239 
  Micky       4241 
  Minnie      4243 
  Donalid     4245 
  Edited      4247 
  Goofy       4249 
  </code></pre>
  
  <p>On sheet 2, there is a similar ID column with only one ID: eg:</p>
  
  <pre><code>ID
  4243
  </code></pre>
  
  <p>I have the vlookup code to match the ID value on sheet2 (in this eg, that would be "4243") with ID value on sheet 1 and copy the name associated with that ID to "Name data" of sheet1. The final result should look like this:</p>
  
  <pre><code>Name       ID    Name Data
  Albert    4235 
  John      4237 
  Robert    4239 
  Micky     4241 
  Minnie    4243     Minnie
  Donalid   4245 
  Edited    4247 
  Goofy     4249
  </code></pre>
  
  <p>Issue: The issue with this code is that it copies the name onto first cell under "Name data". Eg: it does the following:</p>
  
  <pre><code>Name      ID      Name Data
  Albert    4235     Minnie
  John      4237 
  Robert    4239 
  Micky     4241 
  Minnie    4243 
  Donalid   4245 
  Edited    4247 
  Goofy     4249
  </code></pre>
  
  <p>The name "minnie" should get copied on 5th row under "Name data" and not on 1st row (excluding heading rows)</p>
  
  <p>Can anyone please suggest me what to fix in the code?</p>
  
  <p>Thanks a ton! </p>
  
  <p>Here is the code:</p>
  
  <pre><code>Sub lookup()
  x = 2
  Set rng1 = Worksheets("Sheet1").Range("b4:b8")
  Set rng2 = Worksheets("Sheet2").Range("A2:A2")
  For Each c1 In rng1
  For Each c2 In rng2
  If c1.Value = c2.Value Then
  Worksheets("sheet1").Cells(x, 3).Value = c2.Value
  If c1.Value &lt;&gt; c2.Value Then
  Worksheets("Sheet1").Cells(x, 3).Value = Worksheets("Sheet1").Cells(x, 3).Value
  x = x + 1
  End If
  End If
  Next
  Next
  End Sub
  </code></pre>
  
  <p>I know that it has something to do with this line:</p>
  
  <pre><code>Worksheets("sheet1").Cells(x, 3).Value = c2.Value
  </code></pre>
  
  <p>but I am not able to figure out what. </p>