title: Compare Columns using VBA Macro
body: <p>I want to insert corresponding <code>Column C</code> values in <code>Column D</code> when Column A and Column B value matches.</p>
  
  <p>For example:</p>
  
  <p>Column A2 is equal to Column B2, now Column C2 value is posted on Column D2</p>
  
  <p>OR</p>
  
  <p>Column A7 is equal to Column B3 then Column C3 value is posted on Column D3 </p>
  
  <p>For detail please see the screen shot so you have idea what i am trying to do.</p>
  
  <p>[Please click to see the screenshot][1]</p>
  
  <p>The code which i am trying is below but it is not working properly, it is just giving only one cell value:</p>
  
  <pre><code>Private Sub ForComparing_Click()
  
  Dim ws As Worksheet
  Dim cel As Range
  Dim lastRowA As Long, lastRowB As Long, lastRowC As Long
  
  Set ws = ThisWorkbook.Sheets("Sheet1")
  
  With ws
      lastRowA = .Cells(.Rows.Count, "A").End(xlUp).Row 'last row of column A
      lastRowB = .Cells(.Rows.Count, "B").End(xlUp).Row 'last row of column B
      lastRowC = .Cells(.Rows.Count, "C").End(xlUp).Row 'last row of column C
      For Each cel In .Range("A2:A" &amp; lastRowA)   'loop through column A
          'check if cell in column A exists in column B
          If WorksheetFunction.CountIf(.Range("B2:B" &amp; lastRowB), cel) = 0 Then
              .Range("D" &amp; cel.Row) = "No Match"
          Else
              .Range("D" &amp; cel.Row) = .Range("C" &amp; cel.Row)
          End If
      Next
  End With
  
  End Sub
  </code></pre>
  
  <p><strong>Edited 1:</strong></p>
  
  <p>Please see the output of this code below:
  <a href="https://i.stack.imgur.com/HXE4c.jpg" rel="nofollow noreferrer">Click here to see screen shot</a></p>
  
  <p><code>Column A3</code> should compare with <code>Column B5</code> because the value <code>D</code> is equal in this case, and then it should print <code>Column C5</code> value to <code>Column D3</code></p>
  
  <p>Furthermore, it should give value in <code>Column D</code> for every value of <code>Column A</code> but it stop after first 4 values.</p>
  
  <p>Thanks for your time.</p>
  
  <p><strong>Edited 2:</strong></p>
  
  <p><a href="https://i.stack.imgur.com/MGhu6.png" rel="nofollow noreferrer">Please see the screen shot</a></p>
  
  <p>What you have just edited is perfectly right, but i want to do this for each <code>Column A</code> value.</p>
  
  <p>I want to compare each <code>Column A</code> value with <code>Column B</code> and then corresponding <code>Column C</code> value is copied on <code>Column D</code>.</p>